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

Added validation to cluster traffic - as downstream tools like nsc will need to perform some level of validation #224

Merged
merged 2 commits into from
Sep 13, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion v2/account_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@ func (ac *ExternalAuthorization) Validate(vr *ValidationResults) {
}
}

const (
ClusterTrafficSystem = "system"
ClusterTrafficOwner = "owner"
)

type ClusterTraffic string

func (ct ClusterTraffic) Valid() error {
if ct == "" || ct == ClusterTrafficSystem || ct == ClusterTrafficOwner {
return nil
}
return fmt.Errorf("unknown cluster traffic option: %q", ct)
}

// Account holds account specific claims data
type Account struct {
Imports Imports `json:"imports,omitempty"`
Expand All @@ -241,7 +255,7 @@ type Account struct {
Mappings Mapping `json:"mappings,omitempty"`
Authorization ExternalAuthorization `json:"authorization,omitempty"`
Trace *MsgTrace `json:"trace,omitempty"`
ClusterTraffic string `json:"cluster_traffic,omitempty"`
ClusterTraffic ClusterTraffic `json:"cluster_traffic,omitempty"`
Info
GenericFields
}
Expand Down Expand Up @@ -309,6 +323,10 @@ func (a *Account) Validate(acct *AccountClaims, vr *ValidationResults) {
}
a.SigningKeys.Validate(vr)
a.Info.Validate(vr)

if err := a.ClusterTraffic.Valid(); err != nil {
vr.AddError(err.Error())
}
}

// AccountClaims defines the body of an account JWT
Expand Down
28 changes: 28 additions & 0 deletions v2/account_claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,3 +990,31 @@ func TestAccountClaimsTraceDestSampling(t *testing.T) {
})
}
}

func TestClusterTraffic_Valid(t *testing.T) {
type clustertest struct {
input string
ok bool
}

tests := []clustertest{
{input: "", ok: true},
{input: "system", ok: true},
{input: "SYSTEM", ok: false},
{input: "owner", ok: true},
{input: "OWNER", ok: false},
{input: "unknown", ok: false},
{input: "account", ok: false},
}

for _, test := range tests {
ct := ClusterTraffic(test.input)
err := ct.Valid()
if test.ok && err != nil {
t.Fatalf("unexpected err for input %q: %v", test.input, err)
}
if !test.ok && err == nil {
t.Fatalf("expected to fail input %q", test.input)
}
}
}
Loading