Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Policy check #25

Merged
merged 6 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ CLI can be previously authenticated with `gcloud auth application-default login`
Parameters for GKE cluster review can be provided as command parameters or via configuration .yaml file.

```sh
gke-policy-automation [global options] command [command options] [arguments...]
gke-policy [global options] command [command options] [arguments...]
```

For cluster review with manually provided parameters:

```sh
./gke-policy-automation cluster review -p <GCP_PROJECT_ID> -n <CLUSTER_NAME> -l <CLUSTER_LOCATION>
./gke-policy cluster review -p <GCP_PROJECT_ID> -n <CLUSTER_NAME> -l <CLUSTER_LOCATION>
```

and with .yaml file with format:
Expand All @@ -73,13 +73,19 @@ Custom policies can be provided via local directory or remote Github repository.
Example for local directory:

```sh
./gke-policy-automation cluster review -p my_project -n my_cluster -l europe-central2-a --local-policy-dir ./gke-policies/policy
./gke-policy cluster review -p my_project -n my_cluster -l europe-central2-a --local-policy-dir ./gke-policies/policy
```

and for Github repository:

```sh
./gke-policy-automation cluster review -p my_project -n my_cluster -l europe-central2-a --git-policy-repo "https://github.com/google/gke-policy-automation" --git-policy-branch main --git-policy-dir gka-policies/policy
./gke-policy cluster review -p my_project -n my_cluster -l europe-central2-a --git-policy-repo "https://github.com/google/gke-policy-automation" --git-policy-branch main --git-policy-dir gka-policies/policy
```

Policy definition validation can be done with command:

```sh
gke-policy policy check [arguments...]
```

## Test
Expand Down
19 changes: 19 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type PolicyAutomation interface {
Close() error
ClusterReview() error
Version() error
PolicyCheck() error
}

type PolicyAutomationApp struct {
Expand Down Expand Up @@ -131,6 +132,24 @@ func (p *PolicyAutomationApp) Version() error {
return nil
}

func (p *PolicyAutomationApp) PolicyCheck() error {
files, err := p.loadPolicyFiles()
if err != nil {
p.out.ErrorPrint("loading policy files failed: ", err)
log.Errorf("loading policy files failed: %s", err)
return err
}
pa := policy.NewPolicyAgent(p.ctx)
if err := pa.WithFiles(files); err != nil {
p.out.ErrorPrint("could not parse policy files", err)
log.Errorf("could not parse policy files: %s", err)
return err
}
p.out.ColorPrintf("[bold][green] All policies validated correctly \n")
log.Info("All policies validated correctly")
return nil
}

func (p *PolicyAutomationApp) loadPolicyFiles() ([]*policy.PolicyFile, error) {
policyFiles := make([]*policy.PolicyFile, 0)
for _, policyConfig := range p.config.Policies {
Expand Down
25 changes: 25 additions & 0 deletions internal/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewPolicyAutomationCli(p PolicyAutomation) *cli.App {
Commands: []*cli.Command{
CreateClusterCommand(p),
CreateVersionCommand(p),
CreatePolicyCheckCommand(p),
},
}
return app
Expand Down Expand Up @@ -111,6 +112,30 @@ func CreateVersionCommand(p PolicyAutomation) *cli.Command {
}
}

func CreatePolicyCheckCommand(p PolicyAutomation) *cli.Command {
config := &CliConfig{}
return &cli.Command{
Name: "policy",
Usage: "Manages policy files",
Subcommands: []*cli.Command{
{
Name: "check",
Usage: "Validates policy files from defined source",
Flags: (getPolicySourceFlags(config)),
Action: func(c *cli.Context) error {
defer p.Close()
if err := p.LoadCliConfig(config); err != nil {
cli.ShowSubcommandHelp(c)
return err
}
p.PolicyCheck()
return nil
},
},
},
}
}

func getPolicySourceFlags(config *CliConfig) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Expand Down