-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a policy
deny_inherit_secrets
(#214)
- Loading branch information
1 parent
4d24dfe
commit 64672f0
Showing
4 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type DenyInheritSecretsPolicy struct{} | ||
|
||
func (policy *DenyInheritSecretsPolicy) Name() string { | ||
return "deny_inherit_secrets" | ||
} | ||
|
||
func (policy *DenyInheritSecretsPolicy) Apply(ctx context.Context, logE *logrus.Entry, cfg *Config, wf *Workflow) error { | ||
failed := false | ||
for jobName, job := range wf.Jobs { | ||
logE := logE.WithField("job_name", jobName) | ||
if job.Secrets.Inherit() { | ||
failed = true | ||
logE.Error("`secrets: inherit` should not be used. Only required secrets should be passed explicitly") | ||
} | ||
} | ||
if failed { | ||
return errors.New("workflow violates policies") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cli | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/suzuki-shunsuke/logrus-error/logerr" | ||
) | ||
|
||
type JobSecrets struct { | ||
m map[string]string | ||
inherit bool | ||
} | ||
|
||
func (js *JobSecrets) Inherit() bool { | ||
return js != nil && js.inherit | ||
} | ||
|
||
func (js *JobSecrets) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var val interface{} | ||
if err := unmarshal(&val); err != nil { | ||
return err | ||
} | ||
return convJobSecrets(val, js) | ||
} | ||
|
||
func convJobSecrets(src interface{}, dest *JobSecrets) error { //nolint:cyclop | ||
switch p := src.(type) { | ||
case string: | ||
switch p { | ||
case "inherit": | ||
dest.inherit = true | ||
return nil | ||
default: | ||
return logerr.WithFields(errors.New("job secrets must be a map or `inherit`"), logrus.Fields{ //nolint:wrapcheck | ||
"secrets": p, | ||
}) | ||
} | ||
case map[interface{}]interface{}: | ||
m := make(map[string]string, len(p)) | ||
for k, v := range p { | ||
ks, ok := k.(string) | ||
if !ok { | ||
return errors.New("secrets key must be string") | ||
} | ||
vs, ok := v.(string) | ||
if !ok { | ||
return errors.New("secrets value must be string") | ||
} | ||
m[ks] = vs | ||
} | ||
dest.m = m | ||
return nil | ||
case map[string]interface{}: | ||
m := make(map[string]string, len(p)) | ||
for k, v := range p { | ||
vs, ok := v.(string) | ||
if !ok { | ||
return errors.New("secrets value must be string") | ||
} | ||
m[k] = vs | ||
} | ||
dest.m = m | ||
return nil | ||
default: | ||
return errors.New("secrets must be map[string]string or 'inherit'") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters