Skip to content

Commit

Permalink
Fix WorkloadIdentity.TTL handling, jobspec2 testing, and hcl1 vs 2 pa…
Browse files Browse the repository at this point in the history
…rsing (#19024)

* make the little dots consistent
* don't trim delimiter as that over matches
* test jobspec2 package
* copy api/WorkloadIdentity.TTL -> structs
* test ttl parsing
* fix hcl1 v 2 parsing mismatch
* make jobspec(1) tests match jobspec2 tests
  • Loading branch information
schmichael authored Nov 8, 2023
1 parent 9d075c4 commit c4ae91f
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 15 deletions.
6 changes: 3 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ check: ## Lint the source code
@$(MAKE) hclfmt
@if (git status -s | grep -q -e '\.hcl$$' -e '\.nomad$$' -e '\.tf$$'); then echo the following HCL files are out of sync; git status -s | grep -e '\.hcl$$' -e '\.nomad$$' -e '\.tf$$'; exit 1; fi

@echo "==> Check API package is isolated from rest"
@echo "==> Check API package is isolated from rest..."
@cd ./api && if go list --test -f '{{ join .Deps "\n" }}' . | grep github.com/hashicorp/nomad/ | grep -v -e /nomad/api/ -e nomad/api.test; then echo " /api package depends the ^^ above internal nomad packages. Remove such dependency"; exit 1; fi

@echo "==> Check command package does not import structs"
@echo "==> Check command package does not import structs..."
@cd ./command && if go list -f '{{ join .Imports "\n" }}' . | grep github.com/hashicorp/nomad/nomad/structs; then echo " /command package imports the structs pkg. Remove such import"; exit 1; fi

@echo "==> Checking Go mod.."
@echo "==> Checking Go mod..."
@GO111MODULE=on $(MAKE) tidy
@if (git status --porcelain | grep -Eq "go\.(mod|sum)"); then \
echo go.mod or go.sum needs updating; \
Expand Down
1 change: 1 addition & 0 deletions ci/test-core.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"helper/...",
"internal/...",
"jobspec/...",
"jobspec2/...",
"lib/...",
"nomad/auth/...",
"nomad/deploymentwatcher/...",
Expand Down
1 change: 1 addition & 0 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,7 @@ func apiWorkloadIdentityToStructs(in *api.WorkloadIdentity) *structs.WorkloadIde
Env: in.Env,
File: in.File,
ServiceName: in.ServiceName,
TTL: in.TTL,
}
}

Expand Down
5 changes: 5 additions & 0 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4250,7 +4250,9 @@ func TestConversion_ApiConsulConnectToStructs(t *testing.T) {

func Test_apiWorkloadIdentityToStructs(t *testing.T) {
ci.Parallel(t)

must.Nil(t, apiWorkloadIdentityToStructs(nil))

must.Eq(t, &structs.WorkloadIdentity{
Name: "consul/test",
Audience: []string{"consul.io"},
Expand All @@ -4264,19 +4266,22 @@ func Test_apiWorkloadIdentityToStructs(t *testing.T) {
File: false,
ServiceName: "web",
}))

must.Eq(t, &structs.WorkloadIdentity{
Name: "aws",
Audience: []string{"s3"},
Env: true,
File: true,
ChangeMode: "signal",
ChangeSignal: "SIGHUP",
TTL: 2 * time.Hour,
}, apiWorkloadIdentityToStructs(&api.WorkloadIdentity{
Name: "aws",
Audience: []string{"s3"},
Env: true,
File: true,
ChangeMode: "signal",
ChangeSignal: "SIGHUP",
TTL: 2 * time.Hour,
}))
}
1 change: 1 addition & 0 deletions jobspec/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func parseConstraints(result *[]*api.Constraint, list *ast.ObjectList) error {
}

m["Operand"] = api.ConstraintDistinctHosts
m["RTarget"] = strconv.FormatBool(enabled)
}

if property, ok := m[api.ConstraintDistinctProperty]; ok {
Expand Down
2 changes: 0 additions & 2 deletions jobspec/parse_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,6 @@ func parseTemplates(result *[]*api.Template, list *ast.ObjectList) error {
ChangeMode: stringToPtr("restart"),
Splay: timeToPtr(5 * time.Second),
Perms: stringToPtr("0644"),
Uid: pointer.Of(-1),
Gid: pointer.Of(-1),
ErrMissingKey: pointer.Of(false),
}

Expand Down
3 changes: 1 addition & 2 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,6 @@ func TestParse(t *testing.T) {
Splay: timeToPtr(10 * time.Second),
Perms: stringToPtr("0644"),
Envvars: boolToPtr(true),
Uid: intToPtr(-1),
Gid: intToPtr(-1),
VaultGrace: timeToPtr(33 * time.Second),
ErrMissingKey: boolToPtr(true),
},
Expand Down Expand Up @@ -533,6 +531,7 @@ func TestParse(t *testing.T) {
Constraints: []*api.Constraint{
{
Operand: api.ConstraintDistinctHosts,
RTarget: "true",
},
},
},
Expand Down
11 changes: 9 additions & 2 deletions jobspec2/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,14 @@ func TestIdentity(t *testing.T) {
must.NotNil(t, job.TaskGroups[0].Tasks[0].Identity)
must.True(t, job.TaskGroups[0].Tasks[0].Identity.Env)
must.True(t, job.TaskGroups[0].Tasks[0].Identity.File)

must.Len(t, 1, job.TaskGroups[0].Tasks[0].Identities)
must.Eq(t, "foo", job.TaskGroups[0].Tasks[0].Identities[0].Name)
must.Eq(t, []string{"bar"}, job.TaskGroups[0].Tasks[0].Identities[0].Audience)
altID := job.TaskGroups[0].Tasks[0].Identities[0]
must.Eq(t, "foo", altID.Name)
must.Eq(t, []string{"bar"}, altID.Audience)
must.True(t, altID.File)
must.False(t, altID.Env)
must.Eq(t, "signal", altID.ChangeMode)
must.Eq(t, "sighup", altID.ChangeSignal)
must.Eq(t, 2*time.Hour, altID.TTL)
}
8 changes: 6 additions & 2 deletions jobspec2/test-fixtures/identity-compat.nomad.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ job "identitycompat" {
# ensure parsing handles both the default and alternate identities
# properly.
identity {
name = "foo"
aud = ["bar"]
name = "foo"
aud = ["bar"]
file = true
ttl = "2h"
change_mode = "signal"
change_signal = "sighup"
}

resources {
Expand Down
4 changes: 2 additions & 2 deletions tools/missing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func isCoveredOne(p string, pkg string) bool {
}

if strings.HasSuffix(p, "/...") {
prefix := strings.TrimSuffix(p, "/...")
if strings.HasPrefix(pkg, prefix) {
prefix := strings.TrimSuffix(p, "...")
if strings.HasPrefix(pkg+"/", prefix) {
return true
}
}
Expand Down
7 changes: 5 additions & 2 deletions tools/missing/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import (

func Test_isCoveredOne(t *testing.T) {
try := func(p string, exp bool) {
result := isCoveredOne(p, "foo/bar")
must.Eq(t, exp, result)
t.Run(p, func(t *testing.T) {
result := isCoveredOne(p, "foo/bar")
must.Eq(t, exp, result)
})
}
try("baz", false)
try("foo", false)
try("foo/bar/baz", false)
try("foo/bar", true)
try("foo/bar/...", true)
try("foo/b/...", false)
try("foo/...", true)
try("abc/...", false)
}

0 comments on commit c4ae91f

Please sign in to comment.