-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- For the checkboxes below you must check each one to indicate that you either did the relevant task, or considered it and decided there was nothing that needed doing --> Issue #678 Added support for kerberos authentication when calling BBS API's. To do this all we need to do is configure our HttpClient to have `UseDefaultCredentials = true` and NOT set the normal Authorization header that we set. This needs to be set at the time we construct the HttpClient. Similar to how we did the NoSSL support, I added a couple different named HttpClient's into our DI container ("Default" and "Kerberos") configured appropriately. Then I added a new `CreateKerberos()` function to `BbsApiFactory` that doesn't require a username/password. Finally I added a 2nd constructor to `BbsApi` that doesn't require a username/password and doesn't attempt to set the Authorization header. I added a `--kerberos` flag to both `bbs2gh generate-script` and `bbs2gh migrate-repo`. For now this is a hidden option. - [x] Did you write/update appropriate tests - [x] Release notes updated (if appropriate) - [x] Appropriate logging output - [x] Issue linked - [x] Docs updated (or issue created) <!-- For docs we should review the docs at: https://docs.github.com/en/early-access/github/migrating-with-github-enterprise-importer and the README.md in this repo If a doc update is required based on the changes in this PR, it is sufficient to create an issue and link to it here. The doc update can be made later/separately. The process to update the docs can be found here: https://github.com/github/docs-early-access#opening-prs The markdown files are here: https://github.com/github/docs-early-access/tree/main/content/github/migrating-with-github-enterprise-importer -->
- Loading branch information
1 parent
e97ddd6
commit 837f39b
Showing
11 changed files
with
262 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Linq; | ||
using System.Net.Http; | ||
using FluentAssertions; | ||
using Moq; | ||
using OctoshiftCLI.BbsToGithub; | ||
using Xunit; | ||
|
||
namespace OctoshiftCLI.Tests.bbs2gh.Commands | ||
{ | ||
public class BbsApiFactoryTests | ||
{ | ||
private const string BBS_SERVER_URL = "http://bbs.contoso.com:7990"; | ||
|
||
private readonly Mock<OctoLogger> _mockOctoLogger = TestHelpers.CreateMock<OctoLogger>(); | ||
private readonly Mock<EnvironmentVariableProvider> _mockEnvironmentVariableProvider = TestHelpers.CreateMock<EnvironmentVariableProvider>(); | ||
private readonly Mock<IHttpClientFactory> _mockHttpClientFactory = new Mock<IHttpClientFactory>(); | ||
|
||
private readonly BbsApiFactory _bbsApiFactory; | ||
|
||
public BbsApiFactoryTests() | ||
{ | ||
_bbsApiFactory = new BbsApiFactory(_mockOctoLogger.Object, _mockHttpClientFactory.Object, _mockEnvironmentVariableProvider.Object, null, null); | ||
} | ||
|
||
[Fact] | ||
public void Should_Create_BbsApi_For_Source_Bbs_Api_With_Kerberos() | ||
{ | ||
using var httpClient = new HttpClient(); | ||
|
||
_mockHttpClientFactory | ||
.Setup(x => x.CreateClient("Kerberos")) | ||
.Returns(httpClient); | ||
|
||
// Act | ||
var githubApi = _bbsApiFactory.CreateKerberos(BBS_SERVER_URL); | ||
|
||
// Assert | ||
githubApi.Should().NotBeNull(); | ||
httpClient.DefaultRequestHeaders.Accept.First().MediaType.Should().Be("application/json"); | ||
} | ||
|
||
[Fact] | ||
public void Should_Create_BbsApi_For_Source_Bbs_Api_With_Default() | ||
{ | ||
using var httpClient = new HttpClient(); | ||
|
||
_mockHttpClientFactory | ||
.Setup(x => x.CreateClient("Default")) | ||
.Returns(httpClient); | ||
|
||
// Act | ||
var githubApi = _bbsApiFactory.Create(BBS_SERVER_URL, "user", "pass"); | ||
|
||
// Assert | ||
githubApi.Should().NotBeNull(); | ||
httpClient.DefaultRequestHeaders.Accept.First().MediaType.Should().Be("application/json"); | ||
} | ||
} | ||
} |
86 changes: 71 additions & 15 deletions
86
src/OctoshiftCLI.Tests/bbs2gh/Commands/GenerateScriptCommandTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,84 @@ | ||
using System; | ||
using FluentAssertions; | ||
using Moq; | ||
using OctoshiftCLI.BbsToGithub; | ||
using OctoshiftCLI.BbsToGithub.Commands; | ||
using OctoshiftCLI.Contracts; | ||
using Xunit; | ||
|
||
namespace OctoshiftCLI.Tests.BbsToGithub.Commands; | ||
|
||
public class GenerateScriptCommandTests | ||
{ | ||
private const string BBS_SERVER_URL = "http://bbs.contoso.com:7990"; | ||
|
||
private readonly Mock<IServiceProvider> _mockServiceProvider = new(); | ||
private readonly Mock<BbsApiFactory> _mockBbsApiFactory = TestHelpers.CreateMock<BbsApiFactory>(); | ||
private readonly Mock<OctoLogger> _mockOctoLogger = TestHelpers.CreateMock<OctoLogger>(); | ||
private readonly Mock<EnvironmentVariableProvider> _mockEnvironmentVariableProvider = TestHelpers.CreateMock<EnvironmentVariableProvider>(); | ||
private readonly Mock<FileSystemProvider> _mockFileSystemProvider = TestHelpers.CreateMock<FileSystemProvider>(); | ||
private readonly Mock<IVersionProvider> _mockVersionProvider = new(); | ||
|
||
private readonly GenerateScriptCommand _command = new(); | ||
|
||
public GenerateScriptCommandTests() | ||
{ | ||
_mockServiceProvider.Setup(m => m.GetService(typeof(OctoLogger))).Returns(_mockOctoLogger.Object); | ||
_mockServiceProvider.Setup(m => m.GetService(typeof(EnvironmentVariableProvider))).Returns(_mockEnvironmentVariableProvider.Object); | ||
_mockServiceProvider.Setup(m => m.GetService(typeof(FileSystemProvider))).Returns(_mockFileSystemProvider.Object); | ||
_mockServiceProvider.Setup(m => m.GetService(typeof(IVersionProvider))).Returns(_mockVersionProvider.Object); | ||
_mockServiceProvider.Setup(m => m.GetService(typeof(BbsApiFactory))).Returns(_mockBbsApiFactory.Object); | ||
} | ||
|
||
[Fact] | ||
public void Should_Have_Options() | ||
{ | ||
var command = new GenerateScriptCommand(); | ||
command.Should().NotBeNull(); | ||
command.Name.Should().Be("generate-script"); | ||
command.Options.Count.Should().Be(10); | ||
|
||
TestHelpers.VerifyCommandOption(command.Options, "bbs-server-url", true); | ||
TestHelpers.VerifyCommandOption(command.Options, "github-org", true); | ||
TestHelpers.VerifyCommandOption(command.Options, "bbs-username", false); | ||
TestHelpers.VerifyCommandOption(command.Options, "bbs-password", false); | ||
TestHelpers.VerifyCommandOption(command.Options, "bbs-shared-home", false); | ||
TestHelpers.VerifyCommandOption(command.Options, "ssh-user", false); | ||
TestHelpers.VerifyCommandOption(command.Options, "ssh-private-key", false); | ||
TestHelpers.VerifyCommandOption(command.Options, "ssh-port", false); | ||
TestHelpers.VerifyCommandOption(command.Options, "output", false); | ||
TestHelpers.VerifyCommandOption(command.Options, "verbose", false); | ||
_command.Should().NotBeNull(); | ||
_command.Name.Should().Be("generate-script"); | ||
_command.Options.Count.Should().Be(11); | ||
|
||
TestHelpers.VerifyCommandOption(_command.Options, "bbs-server-url", true); | ||
TestHelpers.VerifyCommandOption(_command.Options, "github-org", true); | ||
TestHelpers.VerifyCommandOption(_command.Options, "bbs-username", false); | ||
TestHelpers.VerifyCommandOption(_command.Options, "bbs-password", false); | ||
TestHelpers.VerifyCommandOption(_command.Options, "bbs-shared-home", false); | ||
TestHelpers.VerifyCommandOption(_command.Options, "ssh-user", false); | ||
TestHelpers.VerifyCommandOption(_command.Options, "ssh-private-key", false); | ||
TestHelpers.VerifyCommandOption(_command.Options, "ssh-port", false); | ||
TestHelpers.VerifyCommandOption(_command.Options, "output", false); | ||
TestHelpers.VerifyCommandOption(_command.Options, "kerberos", false, true); | ||
TestHelpers.VerifyCommandOption(_command.Options, "verbose", false); | ||
} | ||
|
||
[Fact] | ||
public void It_Gets_A_Kerberos_HttpClient_When_Kerberos_Is_True() | ||
{ | ||
var args = new GenerateScriptCommandArgs | ||
{ | ||
BbsServerUrl = BBS_SERVER_URL, | ||
Kerberos = true | ||
}; | ||
|
||
_command.BuildHandler(args, _mockServiceProvider.Object); | ||
|
||
_mockBbsApiFactory.Verify(m => m.CreateKerberos(BBS_SERVER_URL)); | ||
} | ||
|
||
[Fact] | ||
public void It_Gets_A_Default_HttpClient_When_Kerberos_Is_Not_Set() | ||
{ | ||
var bbsTestUser = "user"; | ||
var bbsTestPassword = "password"; | ||
|
||
var args = new GenerateScriptCommandArgs | ||
{ | ||
BbsServerUrl = BBS_SERVER_URL, | ||
BbsUsername = bbsTestUser, | ||
BbsPassword = bbsTestPassword, | ||
}; | ||
|
||
_command.BuildHandler(args, _mockServiceProvider.Object); | ||
|
||
_mockBbsApiFactory.Verify(m => m.Create(BBS_SERVER_URL, bbsTestUser, bbsTestPassword)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.