6
6
import org .slf4j .Logger ;
7
7
import org .slf4j .LoggerFactory ;
8
8
9
+ import java .io .FileReader ;
10
+ import java .io .IOException ;
9
11
import java .net .HttpURLConnection ;
10
12
import java .net .URI ;
11
13
import java .net .URISyntaxException ;
14
+ import java .nio .charset .StandardCharsets ;
15
+ import java .nio .file .Files ;
16
+ import java .nio .file .Path ;
17
+ import java .nio .file .Paths ;
12
18
import java .util .Collections ;
13
19
import java .util .HashMap ;
14
20
@@ -45,23 +51,23 @@ private static URI validateAndGetUri(String identityEndpoint, String imdsEndpoin
45
51
} catch (URISyntaxException e ) {
46
52
throw new MsalManagedIdentityException (MsalError .INVALID_MANAGED_IDENTITY_ENDPOINT , String .format (
47
53
MsalErrorMessage .MANAGED_IDENTITY_ENDPOINT_INVALID_URI_ERROR , "IDENTITY_ENDPOINT" , identityEndpoint , AZURE_ARC ),
48
- ManagedIdentitySourceType .AzureArc );
54
+ ManagedIdentitySourceType .AZURE_ARC );
49
55
}
50
56
51
57
LOG .info ("[Managed Identity] Creating Azure Arc managed identity. Endpoint URI: " + endpointUri );
52
58
return endpointUri ;
53
59
}
54
60
55
61
private AzureArcManagedIdentitySource (URI endpoint , MsalRequest msalRequest , ServiceBundle serviceBundle ){
56
- super (msalRequest , serviceBundle , ManagedIdentitySourceType .AzureArc );
62
+ super (msalRequest , serviceBundle , ManagedIdentitySourceType .AZURE_ARC );
57
63
this .MSI_ENDPOINT = endpoint ;
58
64
59
65
ManagedIdentityIdType idType =
60
66
((ManagedIdentityApplication ) msalRequest .application ()).getManagedIdentityId ().getIdType ();
61
- if (idType != ManagedIdentityIdType .SystemAssigned ) {
67
+ if (idType != ManagedIdentityIdType .SYSTEM_ASSIGNED ) {
62
68
throw new MsalManagedIdentityException (MsalError .USER_ASSIGNED_MANAGED_IDENTITY_NOT_SUPPORTED ,
63
69
String .format (MsalErrorMessage .MANAGED_IDENTITY_USER_ASSIGNED_NOT_SUPPORTED , AZURE_ARC ),
64
- ManagedIdentitySourceType .CloudShell );
70
+ ManagedIdentitySourceType .AZURE_ARC );
65
71
}
66
72
}
67
73
@@ -82,31 +88,36 @@ public void createManagedIdentityRequest(String resource)
82
88
@ Override
83
89
public ManagedIdentityResponse handleResponse (
84
90
ManagedIdentityParameters parameters ,
85
- IHttpResponse response )
86
- {
91
+ IHttpResponse response ) {
92
+
87
93
LOG .info ("[Managed Identity] Response received. Status code: {response.StatusCode}" );
88
94
89
- if (response .statusCode () == HttpURLConnection .HTTP_UNAUTHORIZED )
90
- {
91
- if (!response .headers ().containsKey ("WWW-Authenticate" )){
95
+ if (response .statusCode () == HttpURLConnection .HTTP_UNAUTHORIZED ) {
96
+ if (!response .headers ().containsKey ("Www-Authenticate" )) {
92
97
LOG .error ("[Managed Identity] WWW-Authenticate header is expected but not found." );
93
98
throw new MsalManagedIdentityException (MsalError .MANAGED_IDENTITY_REQUEST_FAILED ,
94
99
MsalErrorMessage .MANAGED_IDENTITY_NO_CHALLENGE_ERROR ,
95
- ManagedIdentitySourceType .AzureArc );
100
+ ManagedIdentitySourceType .AZURE_ARC );
96
101
}
97
102
98
- String challenge = response .headers ().get ("WWW -Authenticate" ).get (0 );
103
+ String challenge = response .headers ().get ("Www -Authenticate" ).get (0 );
99
104
String [] splitChallenge = challenge .split ("=" );
100
105
101
- if (splitChallenge .length != 2 )
102
- {
106
+ if (splitChallenge .length != 2 ) {
103
107
LOG .error ("[Managed Identity] The WWW-Authenticate header for Azure arc managed identity is not an expected format." );
104
108
throw new MsalManagedIdentityException (MsalError .MANAGED_IDENTITY_REQUEST_FAILED ,
105
109
MsalErrorMessage .MANAGED_IDENTITY_INVALID_CHALLENGE ,
106
- ManagedIdentitySourceType .AzureArc );
110
+ ManagedIdentitySourceType .AZURE_ARC );
107
111
}
108
112
109
- String authHeaderValue = "Basic " + splitChallenge [1 ];
113
+ Path path = Paths .get (splitChallenge [1 ]);
114
+
115
+ String authHeaderValue = null ;
116
+ try {
117
+ authHeaderValue = "Basic " + new String (Files .readAllBytes (path ), StandardCharsets .UTF_8 );
118
+ } catch (IOException e ) {
119
+ throw new MsalManagedIdentityException (MsalError .MANAGED_IDENTITY_FILE_READ_ERROR , e .getMessage (), ManagedIdentitySourceType .AZURE_ARC );
120
+ }
110
121
111
122
createManagedIdentityRequest (parameters .resource );
112
123
0 commit comments