Skip to content
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
3 changes: 3 additions & 0 deletions docs/pages/includes/role-spec.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,11 @@ spec:

# workload_identity_labels: a user/bot with this role will be allowed to
# issue Workload Identities with labels matching below.
#
# Supports role templating with traits.
workload_identity_labels:
'env': 'prod'
'team': '{{external.team}}'

# node_labels_expression has the same purpose as node_labels but
# supports predicate expressions to configure custom logic.
Expand Down
62 changes: 62 additions & 0 deletions lib/auth/machineid/workloadidentityv1/workloadidentityv1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,34 @@ func TestIssueWorkloadIdentity(t *testing.T) {
specificAccessClient, err := tp.srv.NewClient(auth.TestUser(specificAccess.GetName()))
require.NoError(t, err)

traitAccess, _, err := auth.CreateUserAndRole(
tp.srv.Auth(),
"traity",
[]string{},
[]types.Rule{
types.NewRule(
types.KindWorkloadIdentity,
[]string{types.VerbRead, types.VerbList},
),
},
auth.WithUserMutator(func(user types.User) {
tr := user.GetTraits()
if tr == nil {
tr = map[string][]string{}
}
tr["custom"] = []string{"trait-value-a", "trait-value-b"}
user.SetTraits(tr)
}),
auth.WithRoleMutator(func(role types.Role) {
role.SetWorkloadIdentityLabels(types.Allow, types.Labels{
"trait-label": []string{"{{external.custom}}"},
})
}),
)
require.NoError(t, err)
traitAccessClient, err := tp.srv.NewClient(auth.TestUser(traitAccess.GetName()))
require.NoError(t, err)

// Generate a keypair to generate x509 SVIDs for.
workloadKey, err := cryptosuites.GenerateKeyWithAlgorithm(cryptosuites.ECDSAP256)
require.NoError(t, err)
Expand Down Expand Up @@ -556,6 +584,23 @@ func TestIssueWorkloadIdentity(t *testing.T) {
})
require.NoError(t, err)

traitsRequired, err := tp.srv.Auth().CreateWorkloadIdentity(ctx, &workloadidentityv1pb.WorkloadIdentity{
Kind: types.KindWorkloadIdentity,
Version: types.V1,
Metadata: &headerv1.Metadata{
Name: "traits-required",
Labels: map[string]string{
"trait-label": "trait-value-b",
},
},
Spec: &workloadidentityv1pb.WorkloadIdentitySpec{
Spiffe: &workloadidentityv1pb.WorkloadIdentitySPIFFE{
Id: "/foo",
},
},
})
require.NoError(t, err)

for policy, result := range map[string]error{
"foo": errors.New("missing artifact signature"),
"bar": nil,
Expand Down Expand Up @@ -968,6 +1013,23 @@ func TestIssueWorkloadIdentity(t *testing.T) {
require.Equal(t, cert.NotAfter.Sub(cert.NotBefore), wantTTL+time.Minute)
},
},
{
name: "x509 svid - access via traits in labels",
client: traitAccessClient,
req: &workloadidentityv1pb.IssueWorkloadIdentityRequest{
Name: traitsRequired.GetMetadata().GetName(),
Credential: &workloadidentityv1pb.IssueWorkloadIdentityRequest_X509SvidParams{
X509SvidParams: &workloadidentityv1pb.X509SVIDParams{
PublicKey: workloadKeyPubBytes,
},
},
WorkloadAttrs: workloadAttrs(nil),
},
requireErr: require.NoError,
assert: func(t *testing.T, res *workloadidentityv1pb.IssueWorkloadIdentityResponse) {
require.NotNil(t, res.Credential)
},
},
{
name: "x509 svid - unspecified ttl",
client: wilcardAccessClient,
Expand Down
1 change: 1 addition & 0 deletions lib/services/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ func ApplyTraits(r types.Role, traits map[string][]string) (types.Role, error) {
types.KindWindowsDesktop,
types.KindUserGroup,
types.KindSAMLIdPServiceProvider,
types.KindWorkloadIdentity,
} {
labelMatchers, err := r.GetLabelMatchers(condition, kind)
if err != nil {
Expand Down
Loading