Skip to content

Commit 16c7410

Browse files
authored
feat(azure): added support for multiple azure MSI profiles (#141)
1 parent 501e66c commit 16c7410

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: "\U0001F389 New Product Feature"
2+
body: |-
3+
Added support for multiple Azure MSI profiles. When selecting Azure as the profile auth-provider the user is prompted to enter the clientID for the MSI (managed identity) they would like to use for this profile. The clientID is stored in the profile config file i.e. .dsv.yml.
4+
the clientID is retrieved from the config when ever azure authentication is required.
5+
time: 2024-05-30T13:24:05.15961-07:00

auth/auth.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"net/http"
7+
"os"
78
"reflect"
89
"strings"
910
"time"
@@ -268,6 +269,11 @@ func (a *authenticator) newRequestBody(at AuthType) (*requestBody, error) {
268269
data, stdErr = buildAwsParams(awsProfile)
269270

270271
case FederatedAzure:
272+
clientID := os.Getenv("AZURE_CLIENT_ID")
273+
if clientID == "" {
274+
clientID = viper.GetString("auth.clientID")
275+
os.Setenv("AZURE_CLIENT_ID", clientID)
276+
}
271277
data, stdErr = buildAzureParams()
272278

273279
case FederatedGcp:

auth/auth_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ func TestGetToken_CachedRefreshToken(t *testing.T) {
210210
}
211211

212212
// TODO: need to refactor the code and rewrite
213+
//
214+
//nolint:perfsprint //errors.go needs work to implement here
213215
func TestGetToken(t *testing.T) {
214216
testCases := []struct {
215217
auth string
@@ -218,7 +220,7 @@ func TestGetToken(t *testing.T) {
218220
expectedError error
219221
}{
220222
{"password", "none", nil, fmt.Errorf("error")},
221-
// {"azure", "none", nil, errors.New("error")},
223+
{"azure", "none", nil, fmt.Errorf("error")},
222224
//{"gcp", "none", nil, errors.New("error")},
223225
{"aws", "none", nil, fmt.Errorf("error")},
224226
{"refresh", "none", nil, fmt.Errorf("error")},

commands/cli-config.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ func handleCliConfigInitCmd(vcli vaultcli.CLI, args []string) int {
684684

685685
// Authentication type and authentication data.
686686
authType := viper.GetString(cst.AuthType)
687+
clientID := ""
687688
if authType == "" {
688689
authType, err = promptAuthType()
689690
if err != nil {
@@ -693,7 +694,6 @@ func handleCliConfigInitCmd(vcli vaultcli.CLI, args []string) int {
693694
viper.Set(cst.AuthType, authType)
694695
}
695696
prf.Set(authType, cst.NounAuth, cst.Type)
696-
697697
var user, password, passwordKey, encryptionKeyFileName string
698698

699699
authProvider := viper.GetString(cst.AuthProvider)
@@ -761,6 +761,15 @@ func handleCliConfigInitCmd(vcli vaultcli.CLI, args []string) int {
761761
viper.Set(cst.AuthClientSecret, clientSecret)
762762
}
763763

764+
case authType == string(auth.FederatedAzure):
765+
clientID, err = promptAzureClientID()
766+
if err != nil {
767+
vcli.Out().WriteResponse(nil, errors.New(err))
768+
}
769+
viper.Set("clientID", clientID)
770+
os.Setenv("AZURE_CLIENT_ID", strings.TrimSpace(clientID))
771+
prf.Set(clientID, cst.NounAuth, "clientID")
772+
764773
case auth.AuthType(authType) == auth.FederatedAws:
765774
awsProfile := viper.GetString(cst.AwsProfile)
766775
if awsProfile == "" {
@@ -863,7 +872,6 @@ func handleCliConfigInitCmd(vcli vaultcli.CLI, args []string) int {
863872
ui.Output(authError.Error())
864873
return 1
865874
}
866-
867875
ui.Output("Failed to authenticate, restoring previous config.")
868876
ui.Output("Please check your credentials, or tenant name, or domain name and try again.")
869877
return 1
@@ -942,6 +950,17 @@ type initializeRequest struct {
942950
Password string
943951
}
944952

953+
func promptAzureClientID() (string, error) {
954+
clientID := ""
955+
clientIDPrompt := &survey.Input{Message: "Please enter a clientID for this Azure profile:"}
956+
957+
survErr := survey.AskOne(clientIDPrompt, &clientID)
958+
if survErr != nil {
959+
return "", survErr
960+
}
961+
return strings.TrimSpace(clientID), nil
962+
}
963+
945964
func promptDomain() (string, error) {
946965
var domain string
947966
domainPrompt := &survey.Select{

0 commit comments

Comments
 (0)