|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +namespace Microsoft.Azure.Commands.Common.Compute.Tests |
| 16 | +{ |
| 17 | + using System; |
| 18 | + using System.Collections.Generic; |
| 19 | + using System.IO; |
| 20 | + using Microsoft.IdentityModel.Clients.ActiveDirectory; |
| 21 | + using Microsoft.Rest; |
| 22 | + |
| 23 | + class CredentialManager |
| 24 | + { |
| 25 | + protected CredentialManager() { } |
| 26 | + |
| 27 | + private const string ServicePrincipalEnvVariableName = "AZURE_SERVICE_PRINCIPAL"; |
| 28 | + |
| 29 | + private static readonly string UserId = "UserId".ToLower(); |
| 30 | + private static readonly string Password = "Password".ToLower(); |
| 31 | + private static readonly string AadTenant = "AADTenant".ToLower(); |
| 32 | + private static readonly string Subscription = "SubscriptionId".ToLower(); |
| 33 | + private const string AuthUrl = "https://login.microsoftonline.com/"; |
| 34 | + private const string BaseUrl = "https://management.azure.com/"; |
| 35 | + |
| 36 | + public string ApplicationId { get; private set; } |
| 37 | + public string ApplicationSecret { get; private set; } |
| 38 | + public string TenantId { get; private set; } |
| 39 | + public string SubscriptionId { get; private set; } |
| 40 | + |
| 41 | + public TokenCredentials TokenCredentials |
| 42 | + { |
| 43 | + get |
| 44 | + { |
| 45 | + var clientCredential = new ClientCredential(ApplicationId, ApplicationSecret); |
| 46 | + var context = new AuthenticationContext(Path.Combine(AuthUrl, TenantId)); |
| 47 | + var result = context.AcquireTokenAsync(BaseUrl, clientCredential); |
| 48 | + |
| 49 | + if (result == null) throw new InvalidOperationException("Failed to obtain the token"); |
| 50 | + |
| 51 | + return new TokenCredentials(result.Result.AccessToken); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static CredentialManager FromServicePrincipalEnvVariable(string envVariableName = ServicePrincipalEnvVariableName) |
| 56 | + { |
| 57 | + //AZURE_SERVICE_PRINCIPAL = UserId =< UserGuid >; Password =< Password >; AADTenant =< TenantGuid >; SubscriptionId =< SubscriptionId > |
| 58 | + var spString = Environment.GetEnvironmentVariable(envVariableName); |
| 59 | + |
| 60 | + if (spString == null) throw new ArgumentNullException($"Failed to get environment variable {envVariableName}"); |
| 61 | + |
| 62 | + var sp = new Dictionary<string, string>(); |
| 63 | + var pairs = spString.Trim().Split(';'); |
| 64 | + foreach (var pair in pairs) |
| 65 | + { |
| 66 | + var keyVal = pair.Trim().Split(new[] { '=' }, 2); |
| 67 | + if (keyVal.Length < 2) throw new ArgumentException($"Failed to parse {envVariableName}"); |
| 68 | + sp.Add(keyVal[0].Trim().ToLower(), keyVal[1].Trim()); |
| 69 | + } |
| 70 | + |
| 71 | + if (!sp.ContainsKey(UserId.ToLower())) throw new ArgumentException($"Failed to find {UserId} in {envVariableName}"); |
| 72 | + if (!sp.ContainsKey(Password.ToLower())) throw new ArgumentException($"Failed to find {Password} in {envVariableName}"); |
| 73 | + if (!sp.ContainsKey(AadTenant.ToLower())) throw new ArgumentException($"Failed to find {AadTenant} in {envVariableName}"); |
| 74 | + if (!sp.ContainsKey(Subscription.ToLower())) throw new ArgumentException($"Failed to find {Subscription} in {envVariableName}"); |
| 75 | + |
| 76 | + var credentialManager = new CredentialManager |
| 77 | + { |
| 78 | + ApplicationId = sp[UserId], |
| 79 | + ApplicationSecret = sp[Password], |
| 80 | + TenantId = sp[AadTenant], |
| 81 | + SubscriptionId = sp[Subscription] |
| 82 | + }; |
| 83 | + |
| 84 | + return credentialManager; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments