/// <summary>
        /// Gets http authentication.
        /// </summary>
        /// <returns>PSHttpJobAuthenticationParams instance.</returns>
        private PSHttpJobAuthenticationParams GetAuthenticationParams()
        {
            if (!string.IsNullOrWhiteSpace(this.HttpAuthenticationType))
            {
                var jobAuthentication = new PSHttpJobAuthenticationParams()
                {
                    HttpAuthType = this.HttpAuthenticationType,
                    ClientCertPfx = string.IsNullOrWhiteSpace(JobDynamicParameters.ClientCertificatePfx) ? null : SchedulerUtility.GetCertData(this.ResolvePath(JobDynamicParameters.ClientCertificatePfx), JobDynamicParameters.ClientCertificatePassword),
                    ClientCertPassword = this.JobDynamicParameters.ClientCertificatePassword,
                    Username = this.JobDynamicParameters.BasicUsername,
                    Password = this.JobDynamicParameters.BasicPassword,
                    Secret = this.JobDynamicParameters.OAuthSecret,
                    Tenant = this.JobDynamicParameters.OAuthTenant,
                    Audience = this.JobDynamicParameters.OAuthAudience,
                    ClientId = this.JobDynamicParameters.OAuthClientId
                };

                return jobAuthentication;
            }

            return null;
        }
Example #2
0
        /// <summary>
        /// Populates error action paramenter values to PSJobActionParams.
        /// </summary>
        /// <param name="errorActionType">Error action type e.g. http, https, storage etc</param>
        /// <returns>PSJobActionParams.</returns>
        internal PSJobActionParams GetErrorActionParamsValue(string errorActionType)
        {
            if (!string.IsNullOrWhiteSpace(errorActionType))
            {
                var jobErrorActionType = (SchedulerModels.JobActionType)Enum.Parse(typeof(SchedulerModels.JobActionType), errorActionType, ignoreCase: true);

                var jobErrorAction = new PSJobActionParams()
                {
                    JobActionType = jobErrorActionType,
                };

                switch(jobErrorActionType)
                {
                    case SchedulerModels.JobActionType.Http:
                    case SchedulerModels.JobActionType.Https:
                        var jobErrorActionAuthentication = new PSHttpJobAuthenticationParams()
                        {
                            HttpAuthType = this.JobDynamicParameters.ErrorActionHttpAuthenticationType,
                            ClientCertPfx = string.IsNullOrWhiteSpace(JobDynamicParameters.ErrorActionClientCertificatePfx) ? null : SchedulerUtility.GetCertData(this.ResolvePath(JobDynamicParameters.ErrorActionClientCertificatePfx), JobDynamicParameters.ErrorActionClientCertificatePassword),
                            ClientCertPassword = this.JobDynamicParameters.ErrorActionClientCertificatePassword,
                            Username = this.JobDynamicParameters.ErrorActionBasicUsername,
                            Password = this.JobDynamicParameters.ErrorActionBasicPassword,
                            Secret = this.JobDynamicParameters.ErrorActionOAuthSecret,
                            Tenant = this.JobDynamicParameters.ErrorActionOAuthTenant,
                            Audience = this.JobDynamicParameters.ErrorActionOAuthAudience,
                            ClientId = this.JobDynamicParameters.ErrorActionOAuthClientId
                        };

                        var httpJobErrorAction = new PSHttpJobActionParams()
                        {
                            RequestMethod = this.JobDynamicParameters.ErrorActionMethod,
                            Uri = this.JobDynamicParameters.ErrorActionUri,
                            RequestBody = this.JobDynamicParameters.ErrorActionRequestBody,
                            RequestHeaders = this.JobDynamicParameters.ErrorActionHeaders,
                            RequestAuthentication = jobErrorActionAuthentication
                        };

                        jobErrorAction.HttpJobAction = httpJobErrorAction;
                        break;

                    case SchedulerModels.JobActionType.StorageQueue:
                        var storageQueueErrorAction = new PSStorageJobActionParams()
                        {
                            StorageAccount = this.JobDynamicParameters.ErrorActionStorageAccount,
                            StorageQueueName = this.JobDynamicParameters.ErrorActionStorageQueue,
                            StorageSasToken = this.JobDynamicParameters.ErrorActionStorageSASToken,
                            StorageQueueMessage = this.JobDynamicParameters.ErrorActionQueueMessageBody,
                        };

                        jobErrorAction.StorageJobAction = storageQueueErrorAction;
                        break;

                    case SchedulerModels.JobActionType.ServiceBusQueue:
                        var serviceBusQueueErrorAction = GetServiceBusErrorActionParams();
                        serviceBusQueueErrorAction.QueueName = this.JobDynamicParameters.ErrorActionServiceBusQueueName;
                        jobErrorAction.ServiceBusAction = serviceBusQueueErrorAction;
                        break;

                    case SchedulerModels.JobActionType.ServiceBusTopic:
                        var serviceBusTopicErrorAction = GetServiceBusErrorActionParams();
                        serviceBusTopicErrorAction.TopicPath = this.JobDynamicParameters.ErrorActionServiceBusTopicPath;
                        jobErrorAction.ServiceBusAction = serviceBusTopicErrorAction;
                        break;
                }

                return jobErrorAction;
            }

            return null;
        }
        /// <summary>
        /// Gets http authentication.
        /// </summary>
        /// <param name="updateJobAuthenticationParams">Http authentication properties specified via PowerShell.</param>
        /// <param name="authentication">Existing job http authentication.</param>
        /// <returns>HttpAuthentication object.</returns>
        private HttpAuthentication GetExistingAuthentication(PSHttpJobAuthenticationParams updateJobAuthenticationParams, HttpAuthentication authentication)
        {
            if (updateJobAuthenticationParams == null)
            {
                return null;
            }

            if(updateJobAuthenticationParams.HttpAuthType != null &&
               (authentication == null || 
                authentication.Type == null ||
                !updateJobAuthenticationParams.HttpAuthType.Equals(authentication.Type.ToString(), StringComparison.InvariantCultureIgnoreCase)))
            {
                return this.PopulateHttpAuthentication(updateJobAuthenticationParams);
            }

            switch (authentication.Type)
            {
                case HttpAuthenticationType.ClientCertificate:
                    var clientCertificate = authentication as ClientCertAuthentication;
                    clientCertificate.Pfx = updateJobAuthenticationParams.ClientCertPfx.GetValueOrDefault(defaultValue: clientCertificate.Pfx);
                    clientCertificate.Password = updateJobAuthenticationParams.ClientCertPassword.GetValueOrDefault(defaultValue: clientCertificate.Password);
                    return clientCertificate;

                case HttpAuthenticationType.Basic:
                    var basic = authentication as BasicAuthentication;
                    basic.Username = updateJobAuthenticationParams.Username.GetValueOrDefault(defaultValue: basic.Username);
                    basic.Password = updateJobAuthenticationParams.Password.GetValueOrDefault(defaultValue: basic.Password);
                    return basic;

                case HttpAuthenticationType.ActiveDirectoryOAuth:
                    var adOAuth = authentication as OAuthAuthentication;
                    adOAuth.Audience = updateJobAuthenticationParams.Audience.GetValueOrDefault(defaultValue: adOAuth.Audience);
                    adOAuth.ClientId = updateJobAuthenticationParams.ClientId.GetValueOrDefault(defaultValue: adOAuth.ClientId);
                    adOAuth.Secret = updateJobAuthenticationParams.Secret.GetValueOrDefault(defaultValue: adOAuth.Secret);
                    adOAuth.Tenant = updateJobAuthenticationParams.Tenant.GetValueOrDefault(defaultValue: adOAuth.Tenant);
                    return adOAuth;

                default:
                    return authentication;
            }
        }
        /// <summary>
        /// Get Http job authentication.
        /// </summary>
        /// <param name="authenticationParams">Http authentication properties specified via PowerShell.</param>
        /// <returns>HttpAuthentication object.</returns>
        private HttpAuthentication PopulateHttpAuthentication(PSHttpJobAuthenticationParams authenticationParams)
        {
            if (authenticationParams == null ||
                authenticationParams.HttpAuthType == null ||
                authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationNone, StringComparison.InvariantCultureIgnoreCase))
            {
                return null;
            }
            else if (authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationClientCertificate, StringComparison.InvariantCultureIgnoreCase))
            {
                if(string.IsNullOrWhiteSpace(authenticationParams.ClientCertPfx) ||
                    string.IsNullOrWhiteSpace(authenticationParams.ClientCertPassword))
                {
                    throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidClientCertAuthRequest);
                }

                var clientCert = new ClientCertAuthentication()
                {
                    Type = HttpAuthenticationType.ClientCertificate,
                    Pfx = authenticationParams.ClientCertPfx,
                    Password = authenticationParams.ClientCertPassword
                };

                return clientCert;
            }
            else if (authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationActiveDirectoryOAuth, StringComparison.InvariantCultureIgnoreCase))
            {
                if (string.IsNullOrWhiteSpace(authenticationParams.Tenant) ||
                    string.IsNullOrWhiteSpace(authenticationParams.ClientId) ||
                    string.IsNullOrWhiteSpace(authenticationParams.Secret) ||
                    string.IsNullOrWhiteSpace(authenticationParams.Audience))
                {
                    throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidActiveDirectoryOAuthRequest);
                }

                var adOAuth = new OAuthAuthentication()
                {
                    Type = HttpAuthenticationType.ActiveDirectoryOAuth,
                    Audience = authenticationParams.Audience,
                    ClientId = authenticationParams.ClientId,
                    Secret = authenticationParams.Secret,
                    Tenant = authenticationParams.Tenant
                };

                return adOAuth;
            }
            else if (authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationBasic, StringComparison.InvariantCultureIgnoreCase))
            {
                if(string.IsNullOrWhiteSpace(authenticationParams.Username) ||
                   string.IsNullOrWhiteSpace(authenticationParams.Password))
                {
                    throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidBasicRequest);
                }

                var basic = new BasicAuthentication()
                {
                    Type = HttpAuthenticationType.Basic,
                    Username = authenticationParams.Username,
                    Password = authenticationParams.Password
                };

                return basic;
            }
            else
            {
                throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidAuthenticationType);
            }
        }