Skip to content

Commit

Permalink
Merge pull request #2 from OctopusDeploy/bug-usernameauth
Browse files Browse the repository at this point in the history
User/Pass authentication doesn't work with Octopus Server 2019.1 and up
  • Loading branch information
pawelpabich authored Jun 17, 2019
2 parents 06707a8 + f29536e commit 3cb3cd7
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions source/Octopus.Cli/Commands/ApiCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public abstract class ApiCommand : CommandBase, ICommand
public const string UsernameEnvVar = "OCTOPUS_CLI_USERNAME";
/// <summary>
/// The environment variable that can hold the password
/// </summary>
/// </summary>
public const string PasswordEnvVar = "OCTOPUS_CLI_PASSWORD";
readonly IOctopusClientFactory clientFactory;
readonly IOctopusAsyncRepositoryFactory repositoryFactory;
string apiKey;
string serverBaseUrl;
bool enableDebugging;
bool ignoreSslErrors;

string password;
string username;
readonly OctopusClientOptions clientOptions = new OctopusClientOptions();
Expand All @@ -60,7 +60,7 @@ protected ApiCommand(IOctopusClientFactory clientFactory, IOctopusAsyncRepositor
options.Add("apiKey=", $"[Optional] Your API key. Get this from the user profile page. Your must provide an apiKey or username and password. If the guest account is enabled, a key of API-GUEST can be used. This key can also be set in the {ApiKeyEnvVar} environment variable.", v => apiKey = v);
options.Add("user=", $"[Optional] Username to use when authenticating with the server. Your must provide an apiKey or username and password. This Username can also be set in the {UsernameEnvVar} environment variable.", v => username = v);
options.Add("pass=", $"[Optional] Password to use when authenticating with the server. This Password can also be set in the {PasswordEnvVar} environment variable.", v => password = v);

options.Add("configFile=", "[Optional] Text file of default values, with one 'key = value' per line.", v => ReadAdditionalInputsFromConfigurationFile(v));
options.Add("debug", "[Optional] Enable debug logging", v => enableDebugging = true);
options.Add("ignoreSslErrors", "[Optional] Set this flag if your Octopus Server uses HTTPS but the certificate is not trusted on this machine. Any certificate errors will be ignored. WARNING: this option may create a security vulnerability.", v => ignoreSslErrors = true);
Expand All @@ -78,15 +78,15 @@ protected ApiCommand(IOctopusClientFactory clientFactory, IOctopusAsyncRepositor
protected string ServerBaseUrl => string.IsNullOrWhiteSpace(serverBaseUrl)
? System.Environment.GetEnvironmentVariable(ServerUrlEnvVar)
: serverBaseUrl;

string ApiKey => string.IsNullOrWhiteSpace(apiKey)
? System.Environment.GetEnvironmentVariable(ApiKeyEnvVar)
: apiKey;

string Username => string.IsNullOrWhiteSpace(username)
? System.Environment.GetEnvironmentVariable(UsernameEnvVar)
: username;

string Password => string.IsNullOrWhiteSpace(password)
? System.Environment.GetEnvironmentVariable(PasswordEnvVar)
: password;
Expand All @@ -104,7 +104,7 @@ public async Task Execute(string[] commandLineArguments)
if (printHelp)
{
this.GetHelp(Console.Out, commandLineArguments);

return;
}

Expand Down Expand Up @@ -135,12 +135,18 @@ public async Task Execute(string[] commandLineArguments)
#else
ServicePointManager.ServerCertificateValidationCallback = ServerCertificateValidationCallback;
#endif

commandOutputProvider.PrintMessages = OutputFormat == OutputFormat.Default || enableDebugging;
CliSerilogLogProvider.PrintMessages = commandOutputProvider.PrintMessages;
commandOutputProvider.PrintHeader();

var client = await clientFactory.CreateAsyncClient(endpoint, clientOptions).ConfigureAwait(false);

if (!string.IsNullOrWhiteSpace(Username))
{
await client.Repository.Users.SignIn(Username, Password).ConfigureAwait(false);
}

var serverHasSpaces = await client.ForSystem().HasLink("Spaces").ConfigureAwait(false);

if (!string.IsNullOrEmpty(spaceName))
Expand Down Expand Up @@ -181,9 +187,9 @@ public async Task Execute(string[] commandLineArguments)
commandOutputProvider.Debug("Space name unspecified, process will run in the default space context");
}
}

RepositoryCommonQueries = new OctopusRepositoryCommonQueries(Repository, commandOutputProvider);

if (enableDebugging)
{
Repository.Client.SendingOctopusRequest += request => commandOutputProvider.Debug("{Method:l} {Uri:l}", request.Method, request.Uri);
Expand All @@ -195,11 +201,6 @@ public async Task Execute(string[] commandLineArguments)

commandOutputProvider.Debug("Handshake successful. Octopus version: {Version:l}; API version: {ApiVersion:l}", root.Version, root.ApiVersion);

if (!string.IsNullOrWhiteSpace(Username))
{
await Repository.Users.SignIn(Username, Password).ConfigureAwait(false);
}

var user = await Repository.Users.GetCurrent().ConfigureAwait(false);
if (user != null)
{
Expand Down Expand Up @@ -243,7 +244,7 @@ private void Respond()
}
}
}

static NetworkCredential ParseCredentials(string username, string password)
{
if (string.IsNullOrWhiteSpace(username))
Expand All @@ -269,7 +270,7 @@ protected void SetFlagState(string input, ref bool? setter)
setter = tempBool;
}
}

protected List<string> ReadAdditionalInputsFromConfigurationFile(string configFile)
{
configFile = FileSystem.GetFullPath(configFile);
Expand Down

0 comments on commit 3cb3cd7

Please sign in to comment.