Skip to content

Commit

Permalink
Add doc info on certPath for Service Principal with Certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
bryan-cox committed Oct 23, 2024
1 parent 7ad2b68 commit 1eb8229
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion api/v1beta1/azureclusteridentity_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type AzureClusterIdentitySpec struct {
// ClientSecret is a secret reference which should contain either a Service Principal password or certificate secret.
// +optional
ClientSecret corev1.SecretReference `json:"clientSecret,omitempty"`
// certPath is the path where certicates exist. When set, it takes precedence over ClientSecret for types that uses certs like ServicePrincipalCertificate.
// certPath is the path where certificates exist. When set, it takes precedence over ClientSecret for types that uses certs like ServicePrincipalCertificate.
// +optional
CertPath string `json:"certPath,omitempty"`
// TenantID is the service principal primary tenant id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ spec:
x-kubernetes-map-type: atomic
type: object
certPath:
description: certPath is the path where certicates exist. When set,
description: certPath is the path where certificates exist. When set,
it takes precedence over ClientSecret for types that uses certs
like ServicePrincipalCertificate.
type: string
Expand Down
43 changes: 27 additions & 16 deletions controllers/asosecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"fmt"
"os"

asoconfig "github.com/Azure/azure-service-operator/v2/pkg/common/config"
"github.com/pkg/errors"
Expand Down Expand Up @@ -287,23 +288,33 @@ func (asos *ASOSecretReconciler) createSecretFromClusterIdentity(ctx context.Con
return newASOSecret, nil
}

// Fetch identity secret, if it exists
key = types.NamespacedName{
Namespace: identity.Spec.ClientSecret.Namespace,
Name: identity.Spec.ClientSecret.Name,
}
identitySecret := &corev1.Secret{}
err := asos.Get(ctx, key, identitySecret)
if err != nil {
return nil, errors.Wrap(err, "failed to fetch AzureClusterIdentity secret")
}
if identity.Spec.CertPath != "" {
certsContent, err := os.ReadFile(identity.Spec.CertPath)
if err != nil {
return nil, errors.Wrap(err, "failed to read certificate file")
}

Check warning on line 295 in controllers/asosecret_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/asosecret_controller.go#L292-L295

Added lines #L292 - L295 were not covered by tests

switch identity.Spec.Type {
case infrav1.ServicePrincipal, infrav1.ManualServicePrincipal:
newASOSecret.Data[asoconfig.AzureClientSecret] = identitySecret.Data[scope.AzureSecretKey]
case infrav1.ServicePrincipalCertificate:
newASOSecret.Data[asoconfig.AzureClientCertificate] = identitySecret.Data["certificate"]
newASOSecret.Data[asoconfig.AzureClientCertificatePassword] = identitySecret.Data["password"]
newASOSecret.Data[asoconfig.AzureClientCertificate] = []byte(certsContent)

Check failure on line 297 in controllers/asosecret_controller.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
newASOSecret.Data[asoconfig.AzureClientCertificatePassword] = []byte{}

Check warning on line 298 in controllers/asosecret_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/asosecret_controller.go#L297-L298

Added lines #L297 - L298 were not covered by tests
} else {
// Fetch identity secret, if it exists
key = types.NamespacedName{
Namespace: identity.Spec.ClientSecret.Namespace,
Name: identity.Spec.ClientSecret.Name,
}
identitySecret := &corev1.Secret{}
err := asos.Get(ctx, key, identitySecret)
if err != nil {
return nil, errors.Wrap(err, "failed to fetch AzureClusterIdentity secret")
}

Check warning on line 309 in controllers/asosecret_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/asosecret_controller.go#L308-L309

Added lines #L308 - L309 were not covered by tests

switch identity.Spec.Type {
case infrav1.ServicePrincipal, infrav1.ManualServicePrincipal:
newASOSecret.Data[asoconfig.AzureClientSecret] = identitySecret.Data[scope.AzureSecretKey]
case infrav1.ServicePrincipalCertificate:
newASOSecret.Data[asoconfig.AzureClientCertificate] = identitySecret.Data["certificate"]
newASOSecret.Data[asoconfig.AzureClientCertificatePassword] = identitySecret.Data["password"]

Check warning on line 316 in controllers/asosecret_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/asosecret_controller.go#L314-L316

Added lines #L314 - L316 were not covered by tests
}
}
return newASOSecret, nil
}
18 changes: 18 additions & 0 deletions docs/book/src/topics/identities.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ data:
password: PASSWORD
```

Alternatively, the path to a certificate can be specified instead of the k8s secret:

```yaml
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: AzureClusterIdentity
metadata:
name: example-identity
namespace: default
spec:
type: ServicePrincipalCertificate
tenantID: <azure-tenant-id>
clientID: <client-id-of-SP-identity>
certPath: <path-to-the-cert>
allowedNamespaces:
list:
- <cluster-namespace>
```

## User-Assigned Managed Identity

<aside class="note">
Expand Down

0 comments on commit 1eb8229

Please sign in to comment.