Skip to content

Commit

Permalink
Retain auth info (AADOAuth and Basic) when getting/updating jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
vivsriaus committed Nov 24, 2014
1 parent 250c147 commit d0f2632
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 38 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,12 @@ use and privacy statement at <url> and (c) agree to sharing my contact inf
<value>A valid value for ClientCertificatePfx and ClientCertificatePassword parameters are required for Http scheduler jobs with ClientCertificate authentication type.</value>
</data>
<data name="SchedulerInvalidNoneAuthRequest" xml:space="preserve">
<value>For None authentication type, both ClientCertificatePfx and ClientCertificatePassword parameters should be null</value>
<value>For None authentication type, all authentication related parameters should be null</value>
</data>
<data name="SchedulerInvalidAADOAuthRequest" xml:space="preserve">
<value>A valid value for Tenant, Secret, Audience and Clientid parameters are required for Http scheduler jobs with ActiveDirectoryOAuth authentication type.</value>
</data>
<data name="SchedulerInvalidBasicAuthRequest" xml:space="preserve">
<value>A valid value for Username and Password parameters are required for Http scheduler jobs with Basic authentication type.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,17 @@ public class PSCreateJobParams
public string ClientCertPfx { get; set; }

public string ClientCertPassword { get; set; }

public string Secret { get; set; }

public string Tenant { get; set; }

public string Audience { get; set; }

public string ClientId { get; set; }

public string Username { get; set; }

public string Password { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,29 +315,19 @@ private JobCreateOrUpdateParameters PopulateExistingJobParams(Job job, PSCreateJ
//Job has existing authentication
if (job.Action.Request.Authentication != null)
{
if (jobRequest.HttpAuthType.Equals("ClientCertificate", StringComparison.OrdinalIgnoreCase))
if (!string.IsNullOrEmpty(jobRequest.HttpAuthType))
{
jobUpdateParams.Action.Request.Authentication = SetClientCertAuthentication(jobRequest, jobUpdateParams);
}
else if (jobRequest.HttpAuthType.Equals("None", StringComparison.OrdinalIgnoreCase))
{
if (!string.IsNullOrEmpty(jobRequest.ClientCertPfx) || !string.IsNullOrEmpty(jobRequest.ClientCertPassword))
{
throw new InvalidOperationException(Resources.SchedulerInvalidNoneAuthRequest);
}
else
{
jobUpdateParams.Action.Request.Authentication = null;
}
jobUpdateParams.Action.Request.Authentication = SetHttpAuthentication(jobRequest, jobUpdateParams);
}
//the new request doesn't have any changes to auth, so preserve it
else
{
jobUpdateParams.Action.Request.Authentication = job.Action.Request.Authentication;
}
}
else if (job.Action.Request.Authentication == null && jobRequest.HttpAuthType.Equals("ClientCertificate", StringComparison.OrdinalIgnoreCase))
else if (job.Action.Request.Authentication == null)
{
jobUpdateParams.Action.Request.Authentication = SetClientCertAuthentication(jobRequest, jobUpdateParams);
jobUpdateParams.Action.Request.Authentication = SetHttpAuthentication(jobRequest, jobUpdateParams);
}
}
else if (job.Action.Request == null)
Expand All @@ -346,10 +336,7 @@ private JobCreateOrUpdateParameters PopulateExistingJobParams(Job job, PSCreateJ
jobUpdateParams.Action.Request.Method = jobRequest.Method;
jobUpdateParams.Action.Request.Headers = jobRequest.Headers.ToDictionary();
jobUpdateParams.Action.Request.Body = jobRequest.Body;
if (jobRequest.HttpAuthType.Equals("ClientCertificate", StringComparison.OrdinalIgnoreCase))
{
jobUpdateParams.Action.Request.Authentication = SetClientCertAuthentication(jobRequest, jobUpdateParams);
}
jobUpdateParams.Action.Request.Authentication = SetHttpAuthentication(jobRequest, jobUpdateParams);
}
}
else
Expand All @@ -358,10 +345,7 @@ private JobCreateOrUpdateParameters PopulateExistingJobParams(Job job, PSCreateJ
jobUpdateParams.Action.Request.Method = jobRequest.Method;
jobUpdateParams.Action.Request.Headers = jobRequest.Headers.ToDictionary();
jobUpdateParams.Action.Request.Body = jobRequest.Body;
if (jobRequest.HttpAuthType.Equals("ClientCertificate", StringComparison.OrdinalIgnoreCase))
{
jobUpdateParams.Action.Request.Authentication = SetClientCertAuthentication(jobRequest, jobUpdateParams);
}
jobUpdateParams.Action.Request.Authentication = SetHttpAuthentication(jobRequest, jobUpdateParams);
}
}
else
Expand Down Expand Up @@ -460,21 +444,74 @@ private JobCreateOrUpdateParameters PopulateExistingJobParams(Job job, PSCreateJ
return jobUpdateParams;
}

private HttpAuthentication SetClientCertAuthentication(PSCreateJobParams jobRequest, JobCreateOrUpdateParameters jobUpdateParams)
private HttpAuthentication SetHttpAuthentication(PSCreateJobParams jobRequest, JobCreateOrUpdateParameters jobUpdateParams)
{
if (jobRequest.ClientCertPfx != null && jobRequest.ClientCertPassword != null)
HttpAuthentication httpAuthentication = null;
if (!string.IsNullOrEmpty(jobRequest.HttpAuthType))
{
return new ClientCertAuthentication
switch (jobRequest.HttpAuthType.ToLower())
{
Type = HttpAuthenticationType.ClientCertificate,
Password = jobRequest.ClientCertPassword,
Pfx = jobRequest.ClientCertPfx
};
}
else
{
throw new InvalidOperationException(Resources.SchedulerInvalidClientCertAuthRequest);
case "clientcertificate":
if (jobRequest.ClientCertPfx != null && jobRequest.ClientCertPassword != null)
{
httpAuthentication = new ClientCertAuthentication
{
Type = HttpAuthenticationType.ClientCertificate,
Password = jobRequest.ClientCertPassword,
Pfx = jobRequest.ClientCertPfx
};
}
else
{
throw new InvalidOperationException(Resources.SchedulerInvalidClientCertAuthRequest);
}
break;

case "activedirectoryoauth":
if (jobRequest.Tenant != null && jobRequest.Audience != null && jobRequest.ClientId != null && jobRequest.Secret != null)
{
httpAuthentication = new AADOAuthAuthentication
{
Type = HttpAuthenticationType.ActiveDirectoryOAuth,
Tenant = jobRequest.Tenant,
Audience = jobRequest.Audience,
ClientId = jobRequest.ClientId,
Secret = jobRequest.Secret
};
}
else
{
throw new InvalidOperationException(Resources.SchedulerInvalidAADOAuthRequest);
}
break;

case "basic":
if (jobRequest.Username != null && jobRequest.Password != null)
{
httpAuthentication = new BasicAuthentication
{
Type = HttpAuthenticationType.Basic,
Username = jobRequest.Username,
Password = jobRequest.Password
};
}
else
{
throw new InvalidOperationException(Resources.SchedulerInvalidBasicAuthRequest);
}
break;

case "none":
if (!string.IsNullOrEmpty(jobRequest.ClientCertPfx) || !string.IsNullOrEmpty(jobRequest.ClientCertPassword) ||
!string.IsNullOrEmpty(jobRequest.Tenant) || !string.IsNullOrEmpty(jobRequest.Secret) || !string.IsNullOrEmpty(jobRequest.ClientId) || !string.IsNullOrEmpty(jobRequest.Audience) ||
!string.IsNullOrEmpty(jobRequest.Username) || !string.IsNullOrEmpty(jobRequest.Password))
{
throw new InvalidOperationException(Resources.SchedulerInvalidNoneAuthRequest);
}
break;
}
}
return httpAuthentication;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public class NewSchedulerHttpJobCommand : SchedulerBaseCmdlet

[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = false, ParameterSetName = AuthParamSet, HelpMessage = "The Http Authentication type (None or ClientCertificate).")]
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = RequiredParamSet, HelpMessage = "The Http Authentication type (None or ClientCertificate).")]
[ValidateSet("None", "ClientCertificate", IgnoreCase = true)]
[ValidateSet("None", "ClientCertificate", "ActiveDirectoryOAuth", "Basic", IgnoreCase = true)]
public string HttpAuthenticationType { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = AuthParamSet, HelpMessage = "The pfx of client certificate.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public class SetSchedulerHttpJobCommand : SchedulerBaseCmdlet

[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = false, ParameterSetName = AuthParamSet, HelpMessage = "The Http Authentication type (None or ClientCertificate).")]
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = RequiredParamSet, HelpMessage = "The Http Authentication type (None or ClientCertificate).")]
[ValidateSet("None", "ClientCertificate", IgnoreCase = true)]
[ValidateSet("None", "ClientCertificate", "ActiveDirectoryOAuth", "Basic", IgnoreCase = true)]
public string HttpAuthenticationType { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = AuthParamSet, HelpMessage = "The pfx of client certificate.")]
Expand Down

0 comments on commit d0f2632

Please sign in to comment.