Skip to content

Commit

Permalink
Add user type (local or sso) to tlsca.Identity
Browse files Browse the repository at this point in the history
Even tho the field was there, it was not being set when converting
to/from pkix.Name.

This PR adds a new extension and sets the value when converting.
  • Loading branch information
marcoandredinis committed Oct 25, 2024
1 parent 02bb832 commit 76f5e6f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/tlsca/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,10 @@ var (
// BotInstanceASN1ExtensionOID is an extension that encodes a unique bot
// instance identifier into a certificate.
BotInstanceASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 20}

// UserTypeASN1ExtensionOID is an extension that encodes the user type.
// Its value is either local or sso.
UserTypeASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 21}
)

// Device Trust OIDs.
Expand Down Expand Up @@ -828,6 +832,15 @@ func (id *Identity) Subject() (pkix.Name, error) {
})
}

if id.UserType != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: UserTypeASN1ExtensionOID,
Value: string(id.UserType),
},
)
}

if len(id.AllowedResourceIDs) > 0 {
allowedResourcesStr, err := types.ResourceIDsToString(id.AllowedResourceIDs)
if err != nil {
Expand Down Expand Up @@ -1108,6 +1121,10 @@ func FromSubject(subject pkix.Name, expires time.Time) (*Identity, error) {
if val, ok := attr.Value.(string); ok {
id.PinnedIP = val
}
case attr.Type.Equal(UserTypeASN1ExtensionOID):
if val, ok := attr.Value.(string); ok {
id.UserType = types.UserType(val)
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions lib/tlsca/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,28 @@ func TestIdentity_ToFromSubject(t *testing.T) {
assertStringOID(t, want.CredentialID, DeviceCredentialIDExtensionOID, subj, "CredentialID mismatch")
},
},
{
name: "user type: sso",
identity: &Identity{
Username: "llama", // Required.
Groups: []string{"editor", "viewer"}, // Required.
UserType: "sso",
},
assertSubject: func(t *testing.T, identity *Identity, subj *pkix.Name) {
assertStringOID(t, string(identity.UserType), UserTypeASN1ExtensionOID, subj, "User Type mismatch")
},
},
{
name: "user type: local",
identity: &Identity{
Username: "llama", // Required.
Groups: []string{"editor", "viewer"}, // Required.
UserType: "local",
},
assertSubject: func(t *testing.T, identity *Identity, subj *pkix.Name) {
assertStringOID(t, string(identity.UserType), UserTypeASN1ExtensionOID, subj, "User Type mismatch")
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down

0 comments on commit 76f5e6f

Please sign in to comment.