Skip to content

Commit

Permalink
Rename policies file to just policy (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelicianoTech authored Jan 21, 2023
1 parent 86c0d3b commit f4af54d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
policies.y*ml
policy.y*ml
repositories.y*ml

coverage.txt
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ Simply download the zip for architecture and extract the exe.
## Configuring

**credentials** - the credentials file is `~/.config/warden/creds.yaml`.
The key `githubtoken` should be set to a token that has enough permissions to do what you need.
The key `githubToken` should be set to a token that has enough permissions to do what you need.

**policies** - the policy file, `warden.yml`, should be in the current directory.
You can get started by copying over the example one: `cp example.warden.yml warden.yml`
**policies** - the policy file, `policy.yml`, should be in the current directory.
You can get started by copying over the example one: `cp example.policy.yml policy.yml`


## Features
Expand Down
38 changes: 19 additions & 19 deletions warden/cmd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type UserPermission struct {
Permission string `yaml:"permission"`
}

type PoliciesFile struct {
type PolicyFile struct {
DefaultBranch string `yaml:"defaultBranch"`
Archived bool `yaml:"archived"` // include archived repos in lookup?
License *LicenseRule `yaml:"license"`
Expand All @@ -66,7 +66,7 @@ func (re *RuleError) Error() string {
var (
auditCmd = &cobra.Command{
Use: "audit",
Short: "Validates that 1 or more repos meet a set of policies",
Short: "Validates that 1 or more repos meet a set of policy",
RunE: func(cmd *cobra.Command, args []string) error {

var res []RuleError
Expand All @@ -76,7 +76,7 @@ var (
log.Fatal(err)
}

policies, _, err := loadPoliciesFile(policiesFileFl)
policy, _, err := loadPolicyFile(policyFileFl)
if err != nil {
log.Fatal(err)
}
Expand All @@ -96,22 +96,22 @@ var (

repoResp, _, _ := client.Repositories.Get(context.Background(), repo.Owner, repo.Name)

if repoResp.GetArchived() != policies.Archived {
if repoResp.GetArchived() != policy.Archived {
continue
}

if repoResp.GetDefaultBranch() != policies.DefaultBranch {
if repoResp.GetDefaultBranch() != policy.DefaultBranch {
res = append(res, RuleError{
Repo{org: repo.Owner, repo: repo.Name},
ERR_DEFAULT_BRANCH,
})

fmt.Printf("Error: The default branch should be %s, not %s.\n", policies.DefaultBranch, repoResp.GetDefaultBranch())
fmt.Printf("Error: The default branch should be %s, not %s.\n", policy.DefaultBranch, repoResp.GetDefaultBranch())
}

// if license is to be checked...
if policies.License != nil && policies.License.Scope == repoResp.GetVisibility() || policies.License.Scope == "all" {
if !slices.Contains(policies.License.Names, repoResp.GetLicense().GetKey()) {
if policy.License != nil && policy.License.Scope == repoResp.GetVisibility() || policy.License.Scope == "all" {
if !slices.Contains(policy.License.Names, repoResp.GetLicense().GetKey()) {
res = append(res, RuleError{
Repo{org: repo.Owner, repo: repo.Name},
ERR_LICENSE,
Expand All @@ -120,17 +120,17 @@ var (
}

// if label checks are to happen
if len(policies.Labels) > 0 {
if len(policy.Labels) > 0 {

labels, _, err := client.Issues.ListLabels(context.Background(), repo.Owner, repo.Name, nil)
if err != nil {
return err
}

if policies.LabelStrategy == "available" || policies.LabelStrategy == "" {
if policy.LabelStrategy == "available" || policy.LabelStrategy == "" {

// for each labal we're checking for
for _, label := range policies.Labels {
for _, label := range policy.Labels {

found := false

Expand All @@ -148,14 +148,14 @@ var (
})
}
}
} else if policies.LabelStrategy == "only" {
} else if policy.LabelStrategy == "only" {

// for each labal we're checking for
for _, iLabel := range labels {

found := false

for _, label := range policies.Labels {
for _, label := range policy.Labels {

if label == iLabel.GetName() {
found = true
Expand All @@ -170,23 +170,23 @@ var (
}
}
} else {
return errors.New("The labelStrategy of " + policies.LabelStrategy + " isn't valid.")
return errors.New("The labelStrategy of " + policy.LabelStrategy + " isn't valid.")
}

}

// if access permissions are to be checked...
if len(policies.Access) > 0 {
if len(policy.Access) > 0 {

teams, _, err := client.Repositories.ListTeams(context.Background(), repo.Owner, repo.Name, nil)
if err != nil {
return err
}

if policies.AccessStrategy == "available" || policies.AccessStrategy == "" {
if policy.AccessStrategy == "available" || policy.AccessStrategy == "" {

// for each team we're checking for
for _, user := range policies.Access {
for _, user := range policy.Access {

found := false
matched := false
Expand Down Expand Up @@ -216,7 +216,7 @@ var (
}
}
} else {
return errors.New("The accessStrategy of " + policies.AccessStrategy + " isn't valid.")
return errors.New("The accessStrategy of " + policy.AccessStrategy + " isn't valid.")
}
}
}
Expand All @@ -241,7 +241,7 @@ var (

func init() {

AddPoliciesFileFlag(auditCmd)
AddPolicyFileFlag(auditCmd)
AddRepositoriesFileFlag(auditCmd)

rootCmd.AddCommand(auditCmd)
Expand Down
6 changes: 3 additions & 3 deletions warden/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package cmd

import "github.com/spf13/cobra"

var policiesFileFl string
var policyFileFl string
var repositoriesFileFl string

func AddPoliciesFileFlag(cmd *cobra.Command) {
func AddPolicyFileFlag(cmd *cobra.Command) {

cmd.PersistentFlags().StringVar(&policiesFileFl, "policiesFile", "", "file containing rules (default is ./policies.y[a]ml)")
cmd.PersistentFlags().StringVar(&policyFileFl, "policyFile", "", "file containing rules (default is ./policy.y[a]ml)")
}

func AddRepositoriesFileFlag(cmd *cobra.Command) {
Expand Down
12 changes: 6 additions & 6 deletions warden/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
"gopkg.in/yaml.v3"
)

// loadPoliciesFile tries to intelligently choose a filepath for the
// policiesfile and then return the unmarshalled struct. If customPath is not
// loadPolicyFile tries to intelligently choose a filepath for the
// policy file and then return the unmarshalled struct. If customPath is not
// empty, it will try to use that before the default filenames.
func loadPoliciesFile(customPath string) (*PoliciesFile, []byte, error) {
func loadPolicyFile(customPath string) (*PolicyFile, []byte, error) {

var file PoliciesFile
var file PolicyFile
var foundFile bool
var yamlContent []byte
var err error

possibleFilepaths := []string{"policies."}
possibleFilepaths := []string{"policy."}

if customPath != "" {
possibleFilepaths = append([]string{customPath}, possibleFilepaths...)
Expand All @@ -40,7 +40,7 @@ func loadPoliciesFile(customPath string) (*PoliciesFile, []byte, error) {
}

if !foundFile {
return nil, nil, fmt.Errorf("A policies file was not found. Either './policies.yml' needs to be used or the '--policiesFile' flag set.")
return nil, nil, fmt.Errorf("A policy file was not found. Either './policy.yml' needs to be used or the '--policyFile' flag set.")
}

return &file, yamlContent, nil
Expand Down
6 changes: 3 additions & 3 deletions warden/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
Short: "Validates a Warden file to match the schema",
RunE: func(cmd *cobra.Command, args []string) error {

_, policiesFile, err := loadPoliciesFile(policiesFileFl)
_, policyFile, err := loadPolicyFile(policyFileFl)
if err != nil {
log.Fatal(err)
}
Expand All @@ -32,7 +32,7 @@ var (

var m interface{}

err = yaml.Unmarshal(policiesFile, &m)
err = yaml.Unmarshal(policyFile, &m)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -60,7 +60,7 @@ var (

func init() {

AddPoliciesFileFlag(validateCmd)
AddPolicyFileFlag(validateCmd)

rootCmd.AddCommand(validateCmd)
}

0 comments on commit f4af54d

Please sign in to comment.