Пример #1
0
 static AzureSession()
 {
     ClientFactory = new AzurePowerShellClientFactory();
     AuthenticationFactory = new AuthenticationFactory();
     CurrentContext = new AzureContext();
     CurrentContext.Environment = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud];
 }
 /// <summary>
 /// Creates new ActiveDirectoryClient using WindowsAzureSubscription.
 /// </summary>
 /// <param name="context"></param>
 public ActiveDirectoryClient(AzureContext context)
 {
     AccessTokenCredential creds = (AccessTokenCredential)AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(context);
     GraphClient = AzureSession.ClientFactory.CreateCustomClient<GraphRbacManagementClient>(
         creds.TenantID,
         creds,
         context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.Graph));
 }
Пример #3
0
 static AzureSession()
 {
     SubscriptionTokenCache = new Dictionary<Tuple<Guid, string>, IAccessToken>();
     ClientFactory = new AzurePowerShellClientFactory();
     AuthenticationFactory = new AuthenticationFactory();
     CurrentContext = new AzureContext();
     CurrentContext.Environment = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud];
 }
        public SubscriptionCloudCredentials GetSubscriptionCloudCredentials(AzureContext context)
        {
            if (context.Subscription == null)
            {
                throw new ApplicationException(Resources.InvalidCurrentSubscription);
            }

            var account = context.Account;

            if (!AzureSession.SubscriptionTokenCache.ContainsKey(Tuple.Create(context.Subscription.Id, context.Account.Id)))
            {
                // Try to re-authenticate
                var tenants = context.Subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants)
                    .Intersect(context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants));

                foreach (var tenant in tenants)
                {
                    try
                    {
                        AzureSession.SubscriptionTokenCache[Tuple.Create(context.Subscription.Id, context.Account.Id)] = Authenticate(ref account, context.Environment, tenant, null, ShowDialog.Never);
                        break;
                    }
                    catch
                    {
                        // Skip
                    }
                }
            }

            if (AzureSession.SubscriptionTokenCache.ContainsKey(Tuple.Create(context.Subscription.Id, context.Account.Id)))
            {
                return new AccessTokenCredential(context.Subscription.Id, AzureSession.SubscriptionTokenCache[Tuple.Create(context.Subscription.Id, context.Account.Id)]);
            }
            else if (account != null)
            {
                switch (account.Type)
                {
                    case AzureAccount.AccountType.User:
                    case AzureAccount.AccountType.ServicePrincipal:
                        if (!AzureSession.SubscriptionTokenCache.ContainsKey(Tuple.Create(context.Subscription.Id, account.Id)))
                        {
                            throw new ArgumentException(Resources.InvalidSubscriptionState);
                        }
                        return new AccessTokenCredential(context.Subscription.Id, AzureSession.SubscriptionTokenCache[Tuple.Create(context.Subscription.Id, account.Id)]);

                    case AzureAccount.AccountType.Certificate:
                        var certificate = ProfileClient.DataStore.GetCertificate(account.Id);
                        return new CertificateCloudCredentials(context.Subscription.Id.ToString(), certificate);

                    default:
                        throw new NotImplementedException("Error: couldn't do whatever it is we're trying to do here");
                }
            }
            else
            {
                throw new ArgumentException(Resources.InvalidSubscriptionState);
            }
        }
Пример #5
0
        /// <summary>
        /// Creates new ResourceManagementClient
        /// </summary>
        /// <param name="subscription">Subscription containing resources to manipulate</param>
        public ResourcesClient(AzureContext context)
            : this(
                AzureSession.ClientFactory.CreateClient<ResourceManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager),
                new GalleryTemplatesClient(context),
                AzureSession.ClientFactory.CreateClient<EventsClient>(context, AzureEnvironment.Endpoint.ResourceManager),
                AzureSession.ClientFactory.CreateClient<AuthorizationManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager))
        {

        }
Пример #6
0
        public static void SetCurrentContext(AzureSubscription subscription, AzureEnvironment environment, AzureAccount account)
        {
            if (environment == null)
            {
                if (subscription != null && CurrentContext != null &&
                    subscription.Environment == CurrentContext.Environment.Name)
                {
                    environment = CurrentContext.Environment;
                }
                else
                {
                    environment = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud];
                }

                if (subscription != null)
                {
                    subscription.Environment = environment.Name;
                }
            }

            if (account == null)
            {
                if (subscription != null && CurrentContext != null && subscription.Account != null)
                {
                    if (CurrentContext.Account != null && subscription.Account == CurrentContext.Account.Id)
                    {
                        account = CurrentContext.Account;
                    }
                    else
                    {
                        throw new ArgumentException(Resources.AccountIdDoesntMatchSubscription, "account");
                    }

                    subscription.Account = account.Id;

                }
            }

            if (subscription != null && subscription.Environment != environment.Name)
            {
                throw new ArgumentException(Resources.EnvironmentNameDoesntMatchSubscription, "environment");
            }

            CurrentContext = new AzureContext
            {
                Subscription = subscription,
                Account = account,
                Environment = environment
            };
        }
        public SubscriptionCloudCredentials GetSubscriptionCloudCredentials(AzureContext context)
        {
            if (context.Subscription == null)
            {
                throw new ApplicationException(Resources.InvalidCurrentSubscription);
            }
            
            if (context.Account == null)
            {
                throw new ArgumentException(Resources.InvalidSubscriptionState);
            }

            if (context.Account.Type == AzureAccount.AccountType.Certificate)
            {
                var certificate = ProfileClient.DataStore.GetCertificate(context.Account.Id);
                return new CertificateCloudCredentials(context.Subscription.Id.ToString(), certificate);
            }

            var tenant = context.Subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants)
                  .Intersect(context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants))
                  .FirstOrDefault();

            if (tenant == null)
            {
                throw new ArgumentException(Resources.InvalidSubscriptionState);
            }

            try
            {
                var token = Authenticate(context.Account, context.Environment, tenant, null, ShowDialog.Never);
                return new AccessTokenCredential(context.Subscription.Id, token);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(Resources.InvalidSubscriptionState, ex);
            }
        }
 public SubscriptionCloudCredentials GetSubscriptionCloudCredentials(AzureContext context)
 {
     return new AccessTokenCredential(context.Subscription.Id, Token);
 }
 public RedisCacheClient(AzureContext context)
 {
     _client = AzureSession.ClientFactory.CreateClient<RedisManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager);
 }
Пример #10
0
 /// <summary>
 /// Creates PoliciesClient using WindowsAzureSubscription instance.
 /// </summary>
 /// <param name="subscription">The WindowsAzureSubscription instance</param>
 public AuthorizationClient(AzureContext context)
 {
     ActiveDirectoryClient = new ActiveDirectoryClient(context);
     AuthorizationManagementClient = AzureSession.ClientFactory.CreateClient<AuthorizationManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager);
 }
 public SubscriptionCloudCredentials GetSubscriptionCloudCredentials(AzureContext context)
 {
     return new CertificateCloudCredentials(context.Subscription.Id.ToString(), Certificate);
 }
        public GalleryTemplatesClient(AzureContext context)
            : this(AzureSession.ClientFactory.CreateClient<GalleryClient>(context, AzureEnvironment.Endpoint.Gallery))
        {

        }
Пример #13
0
 public DataFactoryClient(AzureContext context)
 {
     DataPipelineManagementClient = AzureSession.ClientFactory.CreateClient<DataPipelineManagementClient>(
         context, AzureEnvironment.Endpoint.ResourceManager);
 }
 public StreamAnalyticsClient(AzureContext context)
 {
     StreamAnalyticsManagementClient = AzureSession.ClientFactory.CreateClient<StreamAnalyticsManagementClient>(
         context, AzureEnvironment.Endpoint.ResourceManager);
 }
Пример #15
0
 /// <summary>
 /// Creates new ResourceManagementClient
 /// </summary>
 /// <param name="subscription">Context with subscription containing a batch account to manipulate</param>
 public BatchClient(AzureContext context)
     : this(AzureSession.ClientFactory.CreateClient<BatchManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager),
     AzureSession.ClientFactory.CreateClient<ResourceManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager))
 {
 }