Skip to content

Commit 56bd5d9

Browse files
authored
Fix: Add support for nested values in secret data
2 parents 793169a + bf83483 commit 56bd5d9

File tree

2 files changed

+32
-53
lines changed

2 files changed

+32
-53
lines changed

examples/serviceinstance/ups-dynatrace.yaml

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,40 @@
22
apiVersion: v1
33
kind: Secret
44
metadata:
5-
name: dynatrace-credentials
5+
name: my-credentials
66
namespace: crossplane-system
77
type: Opaque
8-
stringData:
9-
environmentid: environmentid
10-
apitoken: apitoken
11-
apiurl: apiurl
12-
type: dynatrace
8+
data:
9+
login: |
10+
{
11+
"username": "admin",
12+
"password": "secret"
13+
}
14+
config: |
15+
{
16+
"database": {
17+
"host": "localhost",
18+
"port": 5432
19+
}
20+
}
1321
1422
---
1523
# UPS with service credentials from a secret ref
1624
apiVersion: cloudfoundry.crossplane.io/v1alpha1
1725
kind: ServiceInstance
1826
metadata:
19-
name: ups-dynatrace
27+
name: my-ups
2028
spec:
2129
forProvider:
2230
type: user-provided
23-
name: ups-dynatrace
31+
name: my-ups
2432
routeServiceUrl: https://my-route-service.example.com
2533
syslogDrainUrl: syslog-tls://example.log-aggregator.com:6514
2634
spaceRef:
2735
name: my-space
2836
policy:
2937
resolve: Always
3038
credentialsSecretRef:
31-
name: dynatrace-credentials
39+
name: my-credentials
3240
namespace: crossplane-system
33-
key: "" # to select the whole secret
34-
35-
---
36-
apiVersion: v1
37-
kind: Secret
38-
metadata:
39-
name: dynatrace-json-credentials
40-
namespace: crossplane-system
41-
type: Opaque
42-
stringData:
43-
credentials: |
44-
{
45-
"environmentid": "environmentid",
46-
"apitoken": "apitoken",
47-
"apiurl": "apiurl",
48-
"type": "dynatrace"
49-
}
5041

51-
---
52-
# UPS with service json credentials from a secret key selector
53-
apiVersion: cloudfoundry.crossplane.io/v1alpha1
54-
kind: ServiceInstance
55-
metadata:
56-
name: ups-ups-json
57-
spec:
58-
forProvider:
59-
type: user-provided
60-
name: ups-dynatrace-json
61-
spaceRef:
62-
name: my-space
63-
policy:
64-
resolve: Always
65-
credentialsSecretRef:
66-
name: dynatrace-json-credentials
67-
namespace: crossplane-system
68-
key: credentials

internal/clients/secretreference.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
1616
)
1717

18-
// SecretRefToJSONRawMessage extracts parameters/credentials from a secret reference.
18+
// ExtractSecret extracts parameters/credentials from a secret reference.
19+
// If a key is specified, returns the raw value for that key.
20+
// If no key is specified, returns all secret data as nested JSON/YAML.
1921
func ExtractSecret(ctx context.Context, kube k8s.Client, sr *xpv1.SecretReference, key string) ([]byte, error) {
2022
if sr == nil {
2123
return nil, nil
@@ -34,14 +36,18 @@ func ExtractSecret(ctx context.Context, kube k8s.Client, sr *xpv1.SecretReferenc
3436
return nil, nil
3537
}
3638

37-
// if key is not specified, return all data from the secret
38-
cred := make(map[string]string)
39+
// if key is not specified, return all data from the secret, also string or nested JSON
40+
data := make(map[string]interface{})
3941
for k, v := range secret.Data {
40-
cred[k] = string(v)
41-
}
42-
buf, err := json.Marshal(cred)
43-
if err != nil {
44-
return nil, err
42+
// Try to parse as JSON first
43+
var jsonValue interface{}
44+
if err := json.Unmarshal(v, &jsonValue); err == nil {
45+
data[k] = jsonValue
46+
} else {
47+
// If not JSON, store as string
48+
data[k] = string(v)
49+
}
4550
}
46-
return buf, nil
51+
52+
return json.Marshal(data)
4753
}

0 commit comments

Comments
 (0)