Skip to content

Commit ef8c754

Browse files
authored
Merge pull request #162 from Rakshith-R/cmdline-audience
Add support for passing audience token through cmdline arguement
2 parents c97af6d + 23f71dc commit ef8c754

File tree

7 files changed

+39
-1
lines changed

7 files changed

+39
-1
lines changed

.github/workflows/integration-test.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ on:
66

77
jobs:
88
minikube-ci:
9-
name: Intergration test
9+
name: Integration test (with${{ matrix.audience == false && 'out' || '' }} audience parameter)
1010
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
audience: [false, true]
1114
steps:
1215
- name: Check out the repo
1316
uses: actions/checkout@v4
@@ -30,6 +33,13 @@ jobs:
3033
run: |
3134
./scripts/deploy-snapshot-controller.sh deploy
3235
36+
- name: Configure audience parameter
37+
run: |
38+
if [ "${{ matrix.audience }}" = "true" ]; then
39+
echo "Enabling audience parameter"
40+
sed -i 's/# - "--audience=test-backup-client"/- "--audience=test-backup-client"/' deploy/example/csi-driver/csi-driver-with-snapshot-metadata-sidecar.yaml
41+
fi
42+
3343
- name: Deploy csi-hostpath-driver
3444
run: |
3545
kubectl apply -f ./client/config/crd/cbt.storage.k8s.io_snapshotmetadataservices.yaml

deploy/example/csi-driver/csi-driver-with-snapshot-metadata-sidecar.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ spec:
2929
- "--csi-address=/csi/csi.sock"
3030
- "--tls-cert=/tmp/certificates/tls.crt"
3131
- "--tls-key=/tmp/certificates/tls.key"
32+
# (optional) audience token string can be passed here.
33+
# If specified, the sidecar will use this instead of
34+
# fetching the token from SnapshotMetadataService CR.
35+
# - "--audience=test-backup-client"
3236
readinessProbe:
3337
exec:
3438
command:

pkg/internal/runtime/runtime.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ type Args struct {
5656
HttpEndpoint string
5757
// MetricsPath is the path where metrics will be recorded
5858
MetricsPath string
59+
// Audience string is used for authentication.
60+
Audience string
5961
}
6062

6163
func (args *Args) Validate() error {
@@ -100,6 +102,7 @@ type Runtime struct {
100102
CSIConn *grpc.ClientConn
101103
MetricsManager metrics.CSIMetricsManager
102104
DriverName string
105+
Audience string
103106
}
104107

105108
// initialize obtains the clients and then the CSI driver name.

pkg/internal/server/grpc/auth.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func (s *Server) authenticateRequest(ctx context.Context, securityToken string)
6969
}
7070

7171
func (s *Server) getAudienceForDriver(ctx context.Context) (string, error) {
72+
if audience := s.audience(); audience != "" {
73+
// If the audience string is set, return it.
74+
return audience, nil
75+
}
76+
7277
sms, err := s.cbtClient().CbtV1alpha1().SnapshotMetadataServices().Get(ctx, s.driverName(), apimetav1.GetOptions{})
7378
if err != nil {
7479
klog.FromContext(ctx).Error(err, msgInternalFailedToFindCR, "driver", s.driverName())

pkg/internal/server/grpc/auth_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ func TestAuthenticateAndAuthorize(t *testing.T) {
4141
assert.Error(t, err)
4242
assert.Empty(t, retAudience)
4343

44+
// set audience in Args
45+
testAudience := "test-audience"
46+
s.config.Runtime.Audience = testAudience
47+
retAudience, err = s.getAudienceForDriver(context.Background())
48+
assert.NoError(t, err)
49+
assert.Equal(t, testAudience, retAudience)
50+
s.config.Runtime.Audience = ""
51+
4452
// fail via authenticateAndAuthorize
4553
err = s.authenticateAndAuthorize(context.Background(), "some-token", "some-namespace")
4654
assert.Error(t, err)

pkg/internal/server/grpc/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ func (s *Server) csiConnection() *grpc.ClientConn {
101101
return s.config.Runtime.CSIConn
102102
}
103103

104+
func (s *Server) audience() string {
105+
return s.config.Runtime.Audience
106+
}
107+
104108
func buildOptions(config ServerConfig) ([]grpc.ServerOption, error) {
105109
tlsOptions, err := buildTLSOption(config.Runtime.TLSCertFile, config.Runtime.TLSKeyFile)
106110
if err != nil {

pkg/sidecar/sidecar.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const (
5555
flagTLSCert = "tls-cert"
5656
flagTLSKey = "tls-key"
5757
flagVersion = "version"
58+
flagAudience = "audience"
5859

5960
// tlsCertEnvVar is an environment variable that specifies the path to tls certificate file.
6061
tlsCertEnvVar = "TLS_CERT_PATH"
@@ -130,6 +131,7 @@ type sidecarFlagSet struct {
130131
showVersion *bool
131132
tlsCert *string
132133
tlsKey *string
134+
audience *string
133135
}
134136

135137
var sidecarFlagSetErrorHandling flag.ErrorHandling = flag.ExitOnError // UT interception point.
@@ -149,6 +151,7 @@ func newSidecarFlagSet(name, version string) *sidecarFlagSet {
149151
s.grpcPort = s.Int(flagGRPCPort, defaultGRPCPort, "GRPC SnapshotMetadata service port number")
150152
s.tlsCert = s.String(flagTLSCert, os.Getenv(tlsCertEnvVar), "Path to the TLS certificate file. Can also be set with the environment variable "+tlsCertEnvVar+".")
151153
s.tlsKey = s.String(flagTLSKey, os.Getenv(tlsKeyEnvVar), "Path to the TLS private key file. Can also be set with the environment variable "+tlsKeyEnvVar+".")
154+
s.audience = s.String(flagAudience, "", "Audience string used for authentication.")
152155

153156
s.maxStreamingDurMin = s.Int(flagMaxStreamingDurationMin, defaultMaxStreamingDurationMin, "The maximum duration in minutes for any individual streaming session")
154157

@@ -193,6 +196,7 @@ func (s *sidecarFlagSet) runtimeArgsFromFlags() runtime.Args {
193196
TLSKeyFile: *s.tlsKey,
194197
HttpEndpoint: *s.httpEndpoint,
195198
MetricsPath: *s.metricsPath,
199+
Audience: *s.audience,
196200
}
197201
}
198202

0 commit comments

Comments
 (0)