Skip to content

Commit 2b18345

Browse files
committed
feat: mount with managed identity auth
1 parent 1e674c2 commit 2b18345

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

pkg/azurefile/azurefile.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ const (
128128
podNameField = "csi.storage.k8s.io/pod.name"
129129
podNamespaceField = "csi.storage.k8s.io/pod.namespace"
130130
serviceAccountTokenField = "csi.storage.k8s.io/serviceAccount.tokens"
131-
clientIDField = "clientID"
132-
tenantIDField = "tenantID"
131+
clientIDField = "clientid"
132+
tenantIDField = "tenantid"
133133
mountOptionsField = "mountoptions"
134134
mountPermissionsField = "mountpermissions"
135135
encryptInTransitField = "encryptintransit"
@@ -777,7 +777,7 @@ func (d *Driver) GetAccountInfo(ctx context.Context, volumeID string, secrets, r
777777

778778
var protocol, accountKey, secretName, pvcNamespace string
779779
// getAccountKeyFromSecret indicates whether get account key only from k8s secret
780-
var getAccountKeyFromSecret, getLatestAccountKey bool
780+
var getAccountKeyFromSecret, getLatestAccountKey, mountWithManagedIdentity bool
781781
var clientID, tenantID, serviceAccountToken string
782782

783783
for k, v := range reqContext {
@@ -808,8 +808,12 @@ func (d *Driver) GetAccountInfo(ctx context.Context, volumeID string, secrets, r
808808
if getLatestAccountKey, err = strconv.ParseBool(v); err != nil {
809809
return rgName, accountName, accountKey, fileShareName, diskName, subsID, fmt.Errorf("invalid %s: %s in volume context", getLatestAccountKeyField, v)
810810
}
811-
case strings.ToLower(clientIDField):
811+
case clientIDField:
812812
clientID = v
813+
case mountWithManagedIdentityField:
814+
if mountWithManagedIdentity, err = strconv.ParseBool(v); err != nil {
815+
return rgName, accountName, accountKey, fileShareName, diskName, subsID, fmt.Errorf("invalid %s: %s in volume context", mountWithManagedIdentityField, v)
816+
}
813817
case strings.ToLower(tenantIDField):
814818
tenantID = v
815819
case strings.ToLower(serviceAccountTokenField):
@@ -839,7 +843,11 @@ func (d *Driver) GetAccountInfo(ctx context.Context, volumeID string, secrets, r
839843
}
840844
}
841845

842-
// if client id is specified, we only use service account token to get account key
846+
if mountWithManagedIdentity {
847+
klog.V(2).Infof("mountWithManagedIdentity is true, use managed identity auth")
848+
return rgName, accountName, accountKey, fileShareName, diskName, subsID, nil
849+
}
850+
843851
if clientID != "" {
844852
klog.V(2).Infof("clientID(%s) is specified, use service account token to get account key", clientID)
845853
accountKey, err := d.cloud.GetStorageAccesskeyFromServiceAccountToken(ctx, subsID, accountName, rgName, clientID, tenantID, serviceAccountToken)

pkg/azurefile/nodeserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu
7777
context := req.GetVolumeContext()
7878
if context != nil {
7979
// token request
80-
if context[serviceAccountTokenField] != "" && getValueInMap(context, clientIDField) != "" {
80+
if !strings.EqualFold(getValueInMap(context, mountWithManagedIdentityField), trueValue) && context[serviceAccountTokenField] != "" && getValueInMap(context, clientIDField) != "" {
8181
klog.V(2).Infof("NodePublishVolume: volume(%s) mount on %s with service account token, clientID: %s", volumeID, target, getValueInMap(context, clientIDField))
8282
_, err := d.NodeStageVolume(ctx, &csi.NodeStageVolumeRequest{
8383
StagingTargetPath: target,

pkg/azurefile/utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ func removeOptionIfExists(options []string, removeOption string) ([]string, bool
377377
}
378378

379379
func setCredentialCache(server, clientID string) ([]byte, error) {
380-
klog.V(2).Infof("setCredentialCache for server %s with clientID %s", server, clientID)
381-
cmd := exec.Command("sudo", "azfilesauthmanager", "set", server, "--imds-client-id", clientID)
380+
cmd := exec.Command("azfilesauthmanager", "set", "https://"+server, "--imds-client-id", clientID)
381+
cmd.Env = append(os.Environ(), cmd.Env...)
382+
klog.V(2).Infof("Executing command: %q", cmd.String())
382383
return cmd.CombinedOutput()
383384
}

pkg/azurefileplugin/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ARG binary=./_output/${ARCH}/azurefileplugin
3434
COPY ${binary} /azurefileplugin
3535
COPY --from=builder --chown=root:root /usr/local/bin/azcopy /usr/local/bin/azcopy
3636

37-
RUN apt update && apt upgrade -y && apt-mark unhold libcap2 && clean-install ca-certificates cifs-utils util-linux e2fsprogs mount udev xfsprogs nfs-common netbase curl sudo python3-requests
37+
RUN apt update && apt upgrade -y && apt-mark unhold libcap2 && clean-install ca-certificates cifs-utils util-linux e2fsprogs mount udev xfsprogs nfs-common netbase curl python3-requests
3838

3939
COPY ./pkg/azurefile-proxy/init.sh /azurefile-proxy/
4040
COPY ./pkg/azurefile-proxy/install-proxy.sh /azurefile-proxy/

0 commit comments

Comments
 (0)