Skip to content

Commit 6518ce5

Browse files
committed
chore(docs): Improve logging and documentation
1 parent 8a23d0e commit 6518ce5

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

docsource/content.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Before continuing, ensure that the following requirements are met:
1212
- `/Status/Endpoints`
1313
- `/Enrollment/CSR`
1414
- `/MetadataFields`
15+
- `/EnrollmentPatterns` (Keyfactor Command 25.1 and above)
1516
- Kubernetes >= v1.19
1617
- [Kubernetes](https://kubernetes.io/docs/tasks/tools/), [Minikube](https://minikube.sigs.k8s.io/docs/start/), [Kind](https://kind.sigs.k8s.io/docs/user/quick-start/), etc.
1718
> You must have permission to create [Custom Resource Definitions](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/) in your Kubernetes cluster.
@@ -58,10 +59,11 @@ Command Issuer enrolls certificates by submitting a POST request to the Command
5859

5960
If your security policy requires fine-grain access control, Command Issuer requires the following Access Rules:
6061

61-
| Global Permissions | Permission Model (Version Two) | Permission Model (Version One) |
62-
|-----------------------------------------|---|---|
63-
| Metadata > Types > Read | `/metadata/types/read/` | `CertificateMetadataTypes:Read` |
64-
| Certificates > Enrollment > Csr | `/certificates/enrollment/csr/` | `CertificateEnrollment:EnrollCSR` |
62+
| Global Permissions | Permission Model (Version Two) | Permission Model (Version One) | Notes
63+
|-----------------------------------------|---|---|--|
64+
| Metadata > Types > Read | `/metadata/types/read/` | `CertificateMetadataTypes:Read` | |
65+
| Certificates > Enrollment > Csr | `/certificates/enrollment/csr/` | `CertificateEnrollment:EnrollCSR` | |
66+
| Enrollment Patterns > Read (Optional) | `/enrollment_pattern/read/` | N/A | Required if using `EnrollmentPatternName` |
6567

6668
> Documentation for [Version Two Permission Model](https://software.keyfactor.com/Core-OnPrem/Current/Content/ReferenceGuide/SecurityRolePermissions.htm#VersionTwoPermissionModel) and [Version One Permission Model](https://software.keyfactor.com/Core-OnPrem/Current/Content/ReferenceGuide/SecurityRolePermissions.htm#VersionOnePermissionModel)
6769
@@ -174,10 +176,6 @@ kubectl -n command-issuer-system create secret generic command-secret \
174176
175177
This section has moved. Please refer to [this link](./docs/ambient-providers/azure.md) for documentation on configuring ambient credentials with AKS.
176178
177-
## Google Kubernetes Engine (GKE) Workload Identity
178-
179-
This section has moved. Please refer to [this link](./docs/ambient-providers/google.md) for documentation on configuring ambient credentials with GKE.
180-
181179
# CA Bundle
182180
183181
If the Command API is configured to use a self-signed certificate or with a certificate whose issuer isn't widely trusted, the CA certificate must be provided as a Kubernetes secret.
@@ -200,6 +198,7 @@ For example, ClusterIssuer resources can be used to issue certificates for resou
200198
export COMMAND_CA_LOGICAL_NAME="<certificateAuthorityName>"
201199
export CERTIFICATE_TEMPLATE_SHORT_NAME="<certificateTemplateShortName>"
202200
export ENROLLMENT_PATTERN_NAME="<enrollmentPatternName>"
201+
export ENROLLMENT_PATTERN_ID="<enrollmentPatternId>"
203202
```
204203

205204
The `spec` field of both the Issuer and ClusterIssuer resources use the following fields:
@@ -211,9 +210,9 @@ For example, ClusterIssuer resources can be used to issue certificates for resou
211210
| caSecretName | (optional) The name of the Kubernetes secret containing the CA certificate. Required if the Command API uses a self-signed certificate or it was signed by a CA that is not widely trusted. |
212211
| certificateAuthorityLogicalName | The logical name of the Certificate Authority to use in Command. For example, `Sub-CA` |
213212
| certificateAuthorityHostname | (optional) The hostname of the Certificate Authority specified by `certificateAuthorityLogicalName`. This field is usually only required if the CA in Command is a DCOM (MSCA-like) CA. |
214-
| enrollmentPatternId | The ID of the [Enrollment Pattern](https://software.keyfactor.com/Core-OnPrem/Current/Content/ReferenceGuide/Enrollment-Patterns.htm) to use when this Issuer/ClusterIssuer enrolls CSRs. **Supported by Keyfactor Command 25.1 and above**. If `certificateTemplate` and `enrollmentPatternName` are both specified, the enrollment pattern parameter will take precedence. If `enrollmentPatternId` and `enrollmentPatternName` are both specified, `enrollmentPatternId` will take precedence. Enrollment will fail if the specified template is not compatible with the enrollment pattern. |
215-
| enrollmentPatternName | The Name of the [Enrollment Pattern](https://software.keyfactor.com/Core-OnPrem/Current/Content/ReferenceGuide/Enrollment-Patterns.htm) to use when this Issuer/ClusterIssuer enrolls CSRs. **Supported by Keyfactor Command 25.1 and above**. If `certificateTemplate` and `enrollmentPatternName` are both specified, the enrollment pattern parameter will take precedence. If `enrollmentPatternId` and `enrollmentPatternName` are both specified, `enrollmentPatternId` will take precedence. Enrollment will fail if the specified template is not compatible with the enrollment pattern. |
216-
| certificateTemplate | The Short Name of the Certificate Template to use when this Issuer/ClusterIssuer enrolls CSRs. **Deprecated in favor of [Enrollment Patterns](https://software.keyfactor.com/Core-OnPrem/Current/Content/WebAPI/KeyfactorAPI/Enrollment-Patterns.htm) as of Keyfactor Command 25.1**. If `certificateTemplate` and `enrollmentPatternName` are both specified, the enrollment pattern parameter will take precedence. Enrollment will fail if the specified template is not compatible with the enrollment pattern. |
213+
| enrollmentPatternId | The ID of the [Enrollment Pattern](https://software.keyfactor.com/Core-OnPrem/Current/Content/ReferenceGuide/Enrollment-Patterns.htm) to use when this Issuer/ClusterIssuer enrolls CSRs. **Supported by Keyfactor Command 25.1 and above**. If `certificateTemplate` and `enrollmentPatternId` are both specified, the enrollment pattern parameter will take precedence. If `enrollmentPatternId` and `enrollmentPatternName` are both specified, `enrollmentPatternId` will take precedence. Enrollment will fail if the specified certificate template is not compatible with the enrollment pattern. |
214+
| enrollmentPatternName | The Name of the [Enrollment Pattern](https://software.keyfactor.com/Core-OnPrem/Current/Content/ReferenceGuide/Enrollment-Patterns.htm) to use when this Issuer/ClusterIssuer enrolls CSRs. **Supported by Keyfactor Command 25.1 and above**. If `certificateTemplate` and `enrollmentPatternName` are both specified, the enrollment pattern parameter will take precedence. If `enrollmentPatternId` and `enrollmentPatternName` are both specified, `enrollmentPatternId` will take precedence. Enrollment will fail if the specified certificate template is not compatible with the enrollment pattern. If using `enrollmentPatternName`, your security role must have `/enrollment_pattern/read/` permission. |
215+
| certificateTemplate | The Short Name of the Certificate Template to use when this Issuer/ClusterIssuer enrolls CSRs. **Deprecated in favor of [Enrollment Patterns](https://software.keyfactor.com/Core-OnPrem/Current/Content/WebAPI/KeyfactorAPI/Enrollment-Patterns.htm) as of Keyfactor Command 25.1**. If `certificateTemplate` and either `enrollmentPatternName` or `enrollmentPatternId` are specified, the enrollment pattern parameter will take precedence. Enrollment will fail if the specified certificate template is not compatible with the enrollment pattern. |
217216
| scopes | (Optional) Required if using ambient credentials with Azure AKS. If using ambient credentials, these scopes will be put on the access token generated by the ambient credentials' token provider, if applicable. |
218217
| audience | (Optional) If using ambient credentials, this audience will be put on the access token generated by the ambient credentials' token provider, if applicable. Google's ambient credential token provider generates an OIDC ID Token. If this value is not provided, it will default to `command`. |
219218
@@ -240,8 +239,9 @@ For example, ClusterIssuer resources can be used to issue certificates for resou
240239
241240
# certificateAuthorityHostname: "$COMMAND_CA_HOSTNAME" # Uncomment if required
242241
certificateAuthorityLogicalName: "$COMMAND_CA_LOGICAL_NAME"
243-
enrollmentPatternName: "$ENROLLMENT_PATTERN_NAME" # Only supported on Keyfactor Command 25.1 and above.
244-
# certificateTemplate: "$CERTIFICATE_TEMPLATE_SHORT_NAME" # Uncomment if required
242+
enrollmentPatternId: "$ENROLLMENT_PATTERN_ID" # Only supported on Keyfactor Command 25.1 and above.
243+
certificateTemplate: "$CERTIFICATE_TEMPLATE_SHORT_NAME" # Required if using Keyfactor Command 24.4 and below.
244+
# enrollmentPatternName: "$ENROLLMENT_PATTERN_NAME" # Only supported on Keyfactor Command 25.1 and above.
245245
# scopes: "openid email https://example.com/.default" # Uncomment if required
246246
# audience: "https://your-command-url.com" # Uncomment if desired
247247
EOF
@@ -267,8 +267,9 @@ For example, ClusterIssuer resources can be used to issue certificates for resou
267267
268268
# certificateAuthorityHostname: "$COMMAND_CA_HOSTNAME" # Uncomment if required
269269
certificateAuthorityLogicalName: "$COMMAND_CA_LOGICAL_NAME"
270-
enrollmentPatternName: "$ENROLLMENT_PATTERN_NAME" # Only supported on Keyfactor Command 25.1 and above.
271-
# certificateTemplate: "$CERTIFICATE_TEMPLATE_SHORT_NAME" # Uncomment if required
270+
enrollmentPatternId: "$ENROLLMENT_PATTERN_ID" # Only supported on Keyfactor Command 25.1 and above.
271+
certificateTemplate: "$CERTIFICATE_TEMPLATE_SHORT_NAME" # Required if using Keyfactor Command 24.4 and below.
272+
# enrollmentPatternName: "$ENROLLMENT_PATTERN_NAME" # Only supported on Keyfactor Command 25.1 and above.
272273
# scopes: "openid email https://example.com/.default" # Uncomment if required
273274
# audience: "https://your-command-url.com" # Uncomment if desired
274275
EOF
@@ -317,7 +318,7 @@ spec:
317318
request: <csr>
318319
```
319320
320-
> All fields in Command Issuer and ClusterIssuer `spec` can be overridden by applying Kubernetes Annotations to Certificates _and_ CertificateRequests. See [runtime customization for more](docs/annotations.md)
321+
> All fields in Command Issuer and ClusterIssuer `spec` can be overridden by applying Kubernetes Annotations to Certificates _and_ CertificateRequests. See [runtime customization for more](#overriding-the-issuerclusterissuer-spec-using-kubernetes-annotations-on-certificaterequest-resources)
321322
322323
## Approving Certificate Requests
323324
@@ -345,7 +346,7 @@ Command Issuer allows you to override the `certificateAuthorityHostname`, `certi
345346
- `command-issuer.keyfactor.com/certificateAuthorityLogicalName` overrides `certificateAuthorityLogicalName`
346347
- `command-issuer.keyfactor.com/certificateTemplate` overrides `certificateTemplate`
347348
- `command-issuer.keyfactor.com/enrollmentPatternName` overrides `enrollmentPatternName`
348-
- `command-issuer.keyfactor.com/enrollmentPatternId` overrides `enrollmentPatternId`
349+
- `command-issuer.keyfactor.com/enrollmentPatternId` overrides `enrollmentPatternId`. Needs to be in string format.
349350

350351
> cert-manager copies Annotations set on Certificate resources to the corresponding CertificateRequest.
351352

internal/command/command.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/pem"
2323
"errors"
2424
"fmt"
25+
"io"
2526
"strconv"
2627
"strings"
2728
"time"
@@ -403,7 +404,7 @@ func (s *signer) Sign(ctx context.Context, csrBytes []byte, config *SignConfig)
403404
config.EnrollmentPatternName = value
404405
}
405406

406-
k8sLog.Info(fmt.Sprintf("Using certificate template %q and certificate authority %q (%s)", config.CertificateTemplate, config.CertificateAuthorityLogicalName, config.CertificateAuthorityHostname))
407+
k8sLog.Info(fmt.Sprintf("Using certificate template %q and certificate authority %q (%s) and enrollment pattern ID %d and enrollment pattern name %s", config.CertificateTemplate, config.CertificateAuthorityLogicalName, config.CertificateAuthorityHostname, config.EnrollmentPatternId, config.EnrollmentPatternName))
407408

408409
csr, err := parseCSR(csrBytes)
409410
if err != nil {
@@ -488,7 +489,13 @@ func (s *signer) Sign(ctx context.Context, csrBytes []byte, config *SignConfig)
488489

489490
req = req.EnrollmentCSREnrollmentRequest(modelRequest)
490491

491-
k8sLog.Info(fmt.Sprintf("Enrolling certificate with Command using template %q and CA %q", config.CertificateTemplate, caBuilder.String()))
492+
// Avoid nil pointer dereference in logs
493+
loggedEnrollmentPatternId := int32(0)
494+
if enrollmentPatternId != nil {
495+
loggedEnrollmentPatternId = *enrollmentPatternId
496+
}
497+
498+
k8sLog.Info(fmt.Sprintf("Enrolling certificate with Command using template %q and CA %q and enrollment pattern ID %d", config.CertificateTemplate, caBuilder.String(), loggedEnrollmentPatternId))
492499

493500
commandCsrResponseObject, _, err := s.client.EnrollCSR(req)
494501
if err != nil {
@@ -571,11 +578,18 @@ func getEnrollmentPatternByName(ctx context.Context, log logr.Logger, s *signer,
571578
pageNumber := 1
572579

573580
for model == nil {
574-
patterns, _, err := s.client.GetEnrollmentPatterns(v1.ApiGetEnrollmentPatternsRequest{}.
581+
patterns, httpResp, err := s.client.GetEnrollmentPatterns(v1.ApiGetEnrollmentPatternsRequest{}.
575582
PageReturned(int32(pageNumber)))
576583

577584
if err != nil {
578-
detail := fmt.Sprintf("error fetching enrollment patterns from Command: %s", err)
585+
// Capture the error message which should indicate the failure reason
586+
msg := ""
587+
if httpResp != nil && httpResp.Body != nil {
588+
defer httpResp.Body.Close()
589+
bodyBytes, _ := io.ReadAll(httpResp.Body)
590+
msg += string(bodyBytes)
591+
}
592+
detail := fmt.Sprintf("error fetching enrollment patterns from Command: %s. Details: %s", err, msg)
579593
return nil, fmt.Errorf("%w: %s: %w", errEnrollmentPatternFailure, detail, err)
580594
}
581595

0 commit comments

Comments
 (0)