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

Add token signing certificate resource #968

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Apply suggestions from code review
  • Loading branch information
manicminer authored Jan 18, 2023
commit 8f264a8959b6d3a2873a59852492bc99ea54a9e1
4 changes: 2 additions & 2 deletions docs/resources/service_principal_token_signing_certificate.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ In addition to all arguments above, the following attributes are exported:

* `start_date` - The start date from which the certificate is valid, formatted as an RFC3339 date string (e.g. `2018-01-01T01:02:03Z`).

* `value` - The certificate data, which is pem encoded but does not include the
* `value` - The certificate data, which is PEM encoded but does not include the
header `-----BEGIN CERTIFICATE-----\n` or the footer `\n-----END CERTIFICATE-----`.

## Import

Token signing certificates can be imported using the object ID of the associated service principal and the key ID of the verify certificate credential, e.g.

```shell
terraform import azuread_service_principal_token_signing_certificate.test 00000000-0000-0000-0000-000000000000/tokenSigningCertificate/11111111-1111-1111-1111-111111111111
terraform import azuread_service_principal_token_signing_certificate.example 00000000-0000-0000-0000-000000000000/tokenSigningCertificate/11111111-1111-1111-1111-111111111111
```

-> This ID format is unique to Terraform and is composed of the service principal's object ID, the string "tokenSigningCertificate" and the verify certificate's key ID in the format `{ServicePrincipalObjectId}/tokenSigningCertificate/{CertificateKeyId}`.
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func servicePrincipalTokenSigningCertificateResource() *schema.Resource {
},

"start_date": {
Description: "The start date from which the certificate is valid, formatted as an RFC3339 date string (e.g. `2018-01-01T01:02:03Z`). If this isn't specified, the current date is used",
Description: "The start date from which the certificate is valid, formatted as an RFC3339 date string (e.g. `2018-01-01T01:02:03Z`).",
Type: schema.TypeString,
Computed: true,
},

"value": {
Description: "The certificate data, which can be PEM encoded, base64 encoded DER or hexadecimal encoded DER",
Description: "The certificate data, which is PEM encoded but does not include the header/footer",
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Expand All @@ -115,13 +115,14 @@ func servicePrincipalTokenSigningCertificateResourceCreate(ctx context.Context,
keyCreds.EndDateTime = &endDate
}

manicminer marked this conversation as resolved.
Show resolved Hide resolved
tf.LockByName(servicePrincipalResourceName, objectId)
defer tf.UnlockByName(servicePrincipalResourceName, objectId)

key, _, err := client.AddTokenSigningCertificate(ctx, objectId, keyCreds)
if err != nil {
return tf.ErrorDiagF(err, "Could not add token signing certificate to service principal with object ID: %q", objectId)
}

tf.LockByName(servicePrincipalResourceName, objectId)
defer tf.UnlockByName(servicePrincipalResourceName, objectId)

// Wait for the credential to appear in the service principal manifest, this can take several minutes
timeout, _ := ctx.Deadline()
Expand Down Expand Up @@ -163,6 +164,9 @@ func servicePrincipalTokenSigningCertificateResourceCreate(ctx context.Context,
}
credential := helpers.GetVerifyKeyCredentialFromCustomKeyId(servicePrincipal.KeyCredentials, *key.CustomKeyIdentifier)

manicminer marked this conversation as resolved.
Show resolved Hide resolved
if credential == nil {
return tf.ErrorDiagF(errors.New("returned credential was nil"), "Could not determine key ID for newly added token signing certificate on service principal %q", objectId)
}
id := parse.NewCredentialID(objectId, "tokenSigningCertificate", *credential.KeyId)

d.SetId(id.String())
Expand Down Expand Up @@ -261,7 +265,6 @@ func servicePrincipalTokenSigningCertificateResourceDelete(ctx context.Context,
}
}
}
log.Printf("[Info] App Password: %v", *app.PasswordCredentials)

newPasswordCredentials := make([]msgraph.PasswordCredential, 0)
if app.PasswordCredentials != nil {
Expand Down