-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from smallstep/herman/policy
Add additional policy options and policy deduplication
- Loading branch information
Showing
12 changed files
with
405 additions
and
76 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
all: generate | ||
all: generate test | ||
|
||
test: | ||
go test -race -coverpkg=./... -covermode=atomic ./... | ||
|
||
generate: | ||
protoc --proto_path=. --go_out=. --go-grpc_out=. --go_opt=paths=source_relative --go-grpc_opt=paths=source_relative provisioners.proto admin.proto config.proto eab.proto majordomo.proto policy.proto | ||
|
||
.PHONY: all generate | ||
.PHONY: all test generate |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,71 @@ | ||
package linkedca | ||
|
||
// Deduplicate removes duplicate values from the Policy | ||
func (p *Policy) Deduplicate() { | ||
if p == nil { | ||
return | ||
} | ||
if x509 := p.GetX509(); x509 != nil { | ||
if allow := x509.GetAllow(); allow != nil { | ||
allow.Dns = removeDuplicates(allow.Dns) | ||
allow.Ips = removeDuplicates(allow.Ips) | ||
allow.Emails = removeDuplicates(allow.Emails) | ||
allow.Uris = removeDuplicates(allow.Uris) | ||
} | ||
if deny := p.GetX509().GetDeny(); deny != nil { | ||
deny.Dns = removeDuplicates(deny.Dns) | ||
deny.Ips = removeDuplicates(deny.Ips) | ||
deny.Emails = removeDuplicates(deny.Emails) | ||
deny.Uris = removeDuplicates(deny.Uris) | ||
} | ||
} | ||
if ssh := p.GetSsh(); ssh != nil { | ||
if host := ssh.GetHost(); host != nil { | ||
if allow := host.GetAllow(); allow != nil { | ||
allow.Dns = removeDuplicates(allow.Dns) | ||
allow.Ips = removeDuplicates(allow.Ips) | ||
allow.Principals = removeDuplicates(allow.Principals) | ||
} | ||
if deny := host.GetDeny(); deny != nil { | ||
deny.Dns = removeDuplicates(deny.Dns) | ||
deny.Ips = removeDuplicates(deny.Ips) | ||
deny.Principals = removeDuplicates(deny.Principals) | ||
} | ||
} | ||
if user := ssh.GetUser(); user != nil { | ||
if allow := user.GetAllow(); allow != nil { | ||
allow.Emails = removeDuplicates(allow.Emails) | ||
allow.Principals = removeDuplicates(allow.Principals) | ||
} | ||
if deny := user.GetDeny(); deny != nil { | ||
deny.Emails = removeDuplicates(deny.Emails) | ||
deny.Principals = removeDuplicates(deny.Principals) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// removeDuplicates returns a new slice of strings with | ||
// duplicate values removed. It retains the order of elements | ||
// in the source slice. | ||
func removeDuplicates(tokens []string) (ret []string) { | ||
|
||
// no need to remove dupes; return original | ||
if len(tokens) <= 1 { | ||
return tokens | ||
} | ||
|
||
keys := make(map[string]struct{}, len(tokens)) | ||
|
||
ret = make([]string, 0, len(tokens)) | ||
for _, item := range tokens { | ||
if _, ok := keys[item]; ok { | ||
continue | ||
} | ||
|
||
keys[item] = struct{}{} | ||
ret = append(ret, item) | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.