Skip to content

Commit 0abae22

Browse files
authored
feat(apikey): support custom metadata in token (#134)
### Motivation Support custom metadata in the token.
1 parent 0570395 commit 0abae22

File tree

6 files changed

+136
-112
lines changed

6 files changed

+136
-112
lines changed

cloud/data_source_apikey.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"context"
1919
"encoding/base64"
2020
"fmt"
21-
"github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
2221
"net/url"
2322
"os"
2423
"strings"
@@ -27,6 +26,7 @@ import (
2726
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2827
"github.com/lestrrat-go/jwx/v2/jwa"
2928
"github.com/lestrrat-go/jwx/v2/jwe"
29+
"github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
3030
"github.com/streamnative/terraform-provider-streamnative/cloud/util"
3131
apierrors "k8s.io/apimachinery/pkg/api/errors"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -77,6 +77,11 @@ func dataSourceApiKey() *schema.Resource {
7777
Computed: true,
7878
Description: descriptions["service_account_name"],
7979
},
80+
"customized_metadata": {
81+
Type: schema.TypeMap,
82+
Computed: true,
83+
Description: descriptions["customized_metadata"],
84+
},
8085
"description": {
8186
Type: schema.TypeString,
8287
Description: descriptions["description"],
@@ -149,6 +154,11 @@ func DataSourceApiKeyRead(ctx context.Context, d *schema.ResourceData, meta inte
149154
if err = d.Set("instance_name", apiKey.Spec.InstanceName); err != nil {
150155
return diag.FromErr(fmt.Errorf("ERROR_SET_INSTANCE_NAME: %w", err))
151156
}
157+
if apiKey.Spec.CustomizedMetadata != nil && len(apiKey.Spec.CustomizedMetadata) > 0 {
158+
if err = d.Set("customized_metadata", apiKey.Spec.CustomizedMetadata); err != nil {
159+
return diag.FromErr(fmt.Errorf("ERROR_SET_CUSTOMIZED_METADATA: %w", err))
160+
}
161+
}
152162
if err = d.Set("service_account_name", apiKey.Spec.ServiceAccountName); err != nil {
153163
return diag.FromErr(fmt.Errorf("ERROR_SET_SERVICE_ACCOUNT_NAME: %w", err))
154164
}

cloud/provider.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,14 @@ func init() {
184184
"rolebinding_condition_resource_names_subscription": "The conditional role binding resource name - subscription",
185185
"rolebinding_condition_resource_names_service_account": "The conditional role binding resource name - service account",
186186
"rolebinding_condition_resource_names_secret": "The conditional role binding resource name - secret",
187-
"volume_name": "The name of the volume",
188-
"bucket": "The bucket name",
189-
"path": "The path of the bucket",
190-
"bucket_region": "The region of the bucket",
191-
"role_arn": "The role arn of the bucket, it is used to access the bucket",
192-
"volume_ready": "Volume is ready, it will be set to 'True' after the volume is ready",
193-
"principal_name": "The principal name of apikey, it is the principal name of the service account that the apikey is associated with, it is used to grant permission on pulsar side",
187+
"volume_name": "The name of the volume",
188+
"bucket": "The bucket name",
189+
"path": "The path of the bucket",
190+
"bucket_region": "The region of the bucket",
191+
"role_arn": "The role arn of the bucket, it is used to access the bucket",
192+
"volume_ready": "Volume is ready, it will be set to 'True' after the volume is ready",
193+
"principal_name": "The principal name of apikey, it is the principal name of the service account that the apikey is associated with, it is used to grant permission on pulsar side",
194+
"customized_metadata": "The custom metadata in the api key token",
194195
}
195196
}
196197

cloud/resource_apikey.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ func resourceApiKey() *schema.Resource {
8686
ForceNew: true,
8787
Description: descriptions["instance_name"],
8888
},
89+
"customized_metadata": {
90+
Type: schema.TypeMap,
91+
Optional: true,
92+
Description: descriptions["customized_metadata"],
93+
},
8994
"token": {
9095
Type: schema.TypeString,
9196
Computed: true,
@@ -221,6 +226,14 @@ func resourceApiKeyCreate(ctx context.Context, d *schema.ResourceData, m interfa
221226
}
222227
revoke := d.Get("revoke").(bool)
223228
ak.Spec.Revoke = revoke
229+
metadata := d.Get("customized_metadata").(map[string]interface{})
230+
if metadata != nil && len(metadata) > 0 {
231+
customizedMetadata := map[string]string{}
232+
for k, v := range metadata {
233+
customizedMetadata[k] = v.(string)
234+
}
235+
ak.Spec.CustomizedMetadata = customizedMetadata
236+
}
224237
_, err = clientSet.CloudV1alpha1().APIKeys(namespace).Create(ctx, ak, metav1.CreateOptions{
225238
FieldManager: "terraform-create",
226239
})
@@ -331,6 +344,13 @@ func resourceApiKeyRead(ctx context.Context, d *schema.ResourceData, m interface
331344
if err = d.Set("organization", apiKey.Namespace); err != nil {
332345
return diag.FromErr(fmt.Errorf("ERROR_SET_ORGANIZATION: %w", err))
333346
}
347+
348+
if apiKey.Spec.CustomizedMetadata != nil && len(apiKey.Spec.CustomizedMetadata) > 0 {
349+
if err = d.Set("customized_metadata", apiKey.Spec.CustomizedMetadata); err != nil {
350+
return diag.FromErr(fmt.Errorf("ERROR_SET_CUSTOMIZED_METADATA: %w", err))
351+
}
352+
}
353+
334354
if err = d.Set("name", apiKey.Name); err != nil {
335355
return diag.FromErr(fmt.Errorf("ERROR_SET_NAME: %w", err))
336356
}

examples/apikey/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ resource "streamnative_apikey" "test-admin-a" {
3535
instance_name = "terraform-test-api-key-pulsar-instance"
3636
service_account_name = "test-tf-admin"
3737
description = "This is a test api key for terraform"
38+
customized_metadata = {
39+
"client_id": "abc"
40+
}
3841
# If you want to revoke the api key, you can set revoke to true
3942
# By default, after revoking an apikey object, all connections using that apikey will
4043
# fail after 1 minute due to an authentication exception.

go.mod

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ require (
1010
github.com/hashicorp/terraform-plugin-sdk/v2 v2.28.0
1111
github.com/lestrrat-go/jwx/v2 v2.0.21
1212
github.com/pkg/errors v0.9.1
13-
github.com/streamnative/cloud-api-server v1.33.1-0.20250815224548-77ed42932280
13+
github.com/streamnative/cloud-api-server v1.33.1-0.20250924010600-2dad16b92d25
1414
github.com/streamnative/cloud-cli v0.22.0-rc.1
1515
github.com/stretchr/testify v1.10.0
1616
github.com/xhit/go-str2duration/v2 v2.1.0
17-
k8s.io/apimachinery v0.30.9
17+
k8s.io/apimachinery v0.32.3
1818
k8s.io/cli-runtime v0.30.9
1919
k8s.io/client-go v12.0.0+incompatible
2020
k8s.io/kubectl v0.30.9
21-
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
21+
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e
2222
)
2323

2424
require (
2525
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
26-
github.com/AthenZ/athenz v1.10.39 // indirect
26+
github.com/AthenZ/athenz v1.12.13 // indirect
2727
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
2828
github.com/DataDog/zstd v1.5.0 // indirect
2929
github.com/MakeNowJust/heredoc v1.0.0 // indirect
@@ -35,7 +35,7 @@ require (
3535
github.com/actgardner/gogen-avro/v10 v10.2.1 // indirect
3636
github.com/agext/levenshtein v1.2.2 // indirect
3737
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirect
38-
github.com/apache/pulsar-client-go v0.13.1 // indirect
38+
github.com/apache/pulsar-client-go v0.16.0-candidate-1.0.20250731021612-06f4dd8bcff0 // indirect
3939
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
4040
github.com/ardielle/ardielle-go v1.5.2 // indirect
4141
github.com/armon/go-radix v1.0.0 // indirect
@@ -62,9 +62,10 @@ require (
6262
github.com/fatih/camelcase v1.0.0 // indirect
6363
github.com/fatih/color v1.13.0 // indirect
6464
github.com/felixge/httpsnoop v1.0.4 // indirect
65-
github.com/fsnotify/fsnotify v1.7.0 // indirect
65+
github.com/fsnotify/fsnotify v1.8.0 // indirect
6666
github.com/fvbommel/sortorder v1.1.0 // indirect
6767
github.com/go-errors/errors v1.4.2 // indirect
68+
github.com/go-jose/go-jose/v4 v4.0.5 // indirect
6869
github.com/go-logr/logr v1.4.2 // indirect
6970
github.com/go-logr/stdr v1.2.2 // indirect
7071
github.com/go-openapi/jsonpointer v0.21.0 // indirect
@@ -79,15 +80,15 @@ require (
7980
github.com/google/btree v1.0.1 // indirect
8081
github.com/google/cel-go v0.21.0 // indirect
8182
github.com/google/gnostic-models v0.6.8 // indirect
82-
github.com/google/go-cmp v0.6.0 // indirect
83+
github.com/google/go-cmp v0.7.0 // indirect
8384
github.com/google/go-jsonnet v0.20.0 // indirect
8485
github.com/google/gofuzz v1.2.0 // indirect
8586
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
8687
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
8788
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
8889
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
8990
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
90-
github.com/hamba/avro/v2 v2.22.2-0.20240625062549-66aad10411d9 // indirect
91+
github.com/hamba/avro/v2 v2.26.0 // indirect
9192
github.com/hashicorp/errwrap v1.1.0 // indirect
9293
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
9394
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
@@ -143,13 +144,13 @@ require (
143144
github.com/olekukonko/tablewriter v0.0.5 // indirect
144145
github.com/onsi/gomega v1.35.1 // indirect
145146
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
146-
github.com/pierrec/lz4 v2.5.2+incompatible // indirect
147+
github.com/pierrec/lz4/v4 v4.1.22 // indirect
147148
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
148149
github.com/posener/complete v1.2.3 // indirect
149-
github.com/prometheus/client_golang v1.19.1 // indirect
150-
github.com/prometheus/client_model v0.5.0 // indirect
151-
github.com/prometheus/common v0.48.0 // indirect
152-
github.com/prometheus/procfs v0.12.0 // indirect
150+
github.com/prometheus/client_golang v1.20.5 // indirect
151+
github.com/prometheus/client_model v0.6.1 // indirect
152+
github.com/prometheus/common v0.55.0 // indirect
153+
github.com/prometheus/procfs v0.15.1 // indirect
153154
github.com/rivo/uniseg v0.4.7 // indirect
154155
github.com/russross/blackfriday v1.6.0 // indirect
155156
github.com/russross/blackfriday/v2 v2.1.0 // indirect
@@ -162,10 +163,10 @@ require (
162163
github.com/spf13/pflag v1.0.5 // indirect
163164
github.com/stoewer/go-strcase v1.3.0 // indirect
164165
github.com/streamnative/function-mesh/api v0.0.0-20240802074023-ee53ec49a51d // indirect
165-
github.com/streamnative/sn-operator/api v0.13.0-rc.5 // indirect
166-
github.com/streamnative/sn-operator/api/commons v0.13.0-rc.5 // indirect
167-
github.com/streamnative/sn-operator/pkg/commons v0.13.0-rc.5 // indirect
168-
github.com/streamnative/unified-rbac/sdk/sdk-go v0.13.0 // indirect
166+
github.com/streamnative/sn-operator/api v0.13.0-rc.15 // indirect
167+
github.com/streamnative/sn-operator/api/commons v0.13.0-rc.15 // indirect
168+
github.com/streamnative/sn-operator/pkg/commons v0.13.0-rc.15 // indirect
169+
github.com/streamnative/unified-rbac/sdk/sdk-go v0.14.0 // indirect
169170
github.com/stripe/stripe-go/v74 v74.5.0 // indirect
170171
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
171172
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
@@ -175,14 +176,14 @@ require (
175176
go.etcd.io/etcd/api/v3 v3.5.10 // indirect
176177
go.etcd.io/etcd/client/pkg/v3 v3.5.10 // indirect
177178
go.etcd.io/etcd/client/v3 v3.5.10 // indirect
178-
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
179-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
180-
go.opentelemetry.io/otel v1.27.0 // indirect
179+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0 // indirect
180+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect
181+
go.opentelemetry.io/otel v1.34.0 // indirect
181182
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.27.0 // indirect
182183
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.27.0 // indirect
183-
go.opentelemetry.io/otel/metric v1.27.0 // indirect
184+
go.opentelemetry.io/otel/metric v1.34.0 // indirect
184185
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
185-
go.opentelemetry.io/otel/trace v1.27.0 // indirect
186+
go.opentelemetry.io/otel/trace v1.34.0 // indirect
186187
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
187188
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
188189
go.uber.org/atomic v1.11.0 // indirect
@@ -197,12 +198,12 @@ require (
197198
golang.org/x/sys v0.31.0 // indirect
198199
golang.org/x/term v0.30.0 // indirect
199200
golang.org/x/text v0.23.0 // indirect
200-
golang.org/x/time v0.7.0 // indirect
201+
golang.org/x/time v0.10.0 // indirect
201202
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
202203
google.golang.org/appengine v1.6.8 // indirect
203-
google.golang.org/genproto/googleapis/api v0.0.0-20240520151616-dc85e6b867a5 // indirect
204-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
205-
google.golang.org/grpc v1.64.1 // indirect
204+
google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb // indirect
205+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250227231956-55c901821b1e // indirect
206+
google.golang.org/grpc v1.71.0 // indirect
206207
google.golang.org/protobuf v1.36.6 // indirect
207208
gopkg.in/inf.v0 v0.9.1 // indirect
208209
gopkg.in/yaml.v2 v2.4.0 // indirect

0 commit comments

Comments
 (0)