|
| 1 | +package com.walmartlabs.concord.repository; |
| 2 | + |
| 3 | +/*- |
| 4 | + * ***** |
| 5 | + * Concord |
| 6 | + * ----- |
| 7 | + * Copyright (C) 2017 - 2025 Walmart Inc. |
| 8 | + * ----- |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * ===== |
| 21 | + */ |
| 22 | + |
| 23 | +import com.walmartlabs.concord.common.AuthTokenProvider; |
| 24 | +import com.walmartlabs.concord.common.secret.BinaryDataSecret; |
| 25 | +import com.walmartlabs.concord.sdk.Secret; |
| 26 | +import org.immutables.value.Value; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | + |
| 31 | +class GitUriTest { |
| 32 | + private static final AuthTokenProvider AUTH_PROVIDER = authProvider(null); |
| 33 | + private static final AuthTokenProvider RESTRICTED_AUTH_PROVIDER = authProvider("gitserver.local"); |
| 34 | + |
| 35 | + private static final GitClientConfiguration cfg = GitClientConfiguration.builder() |
| 36 | + .oauthToken("mock-token") |
| 37 | + .build(); |
| 38 | + private static final GitClient client = new GitClient(cfg, AUTH_PROVIDER); |
| 39 | + private static Secret secret = new BinaryDataSecret("secret-mock-token".getBytes()); |
| 40 | + |
| 41 | + private static AuthTokenProvider authProvider(String urlPattern) { |
| 42 | + var builder = TestOauthTokenConfig.builder() |
| 43 | + .oauthToken("mock-token"); |
| 44 | + |
| 45 | + if (urlPattern != null) { |
| 46 | + builder.oauthUrlPattern(urlPattern); |
| 47 | + } |
| 48 | + |
| 49 | + return new AuthTokenProvider.OauthTokenProvider(builder.build()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testSsh() { |
| 54 | + var sshWithUser = client. updateUrl( "[email protected]:my-org/my-repo.git", null); |
| 55 | + assertEquals( "[email protected]:my-org/my-repo.git", sshWithUser); |
| 56 | + |
| 57 | + var sshNoUser = client.updateUrl("gitserver.local:my-org/my-repo.git", null); |
| 58 | + assertEquals("gitserver.local:my-org/my-repo.git", |
| 59 | + sshNoUser); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testHttps() { |
| 64 | + var httpsDefault = client.updateUrl("https://gitserver.local/my-org/my-repo.git", null); |
| 65 | + assertEquals( "https://[email protected]/my-org/my-repo.git", httpsDefault); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testHttpWithSecret() { |
| 70 | + var httpsSecret = client.updateUrl("https://gitserver.local/my-org/my-repo.git", secret); |
| 71 | + assertEquals( "https://[email protected]/my-org/my-repo.git", httpsSecret); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testUnrestrictedHost() { |
| 76 | + var anonAuth = client.updateUrl("https://elsewhere.local/my-org/my-repo.git", null); |
| 77 | + // backwards-compat, auth added |
| 78 | + assertEquals( "https://[email protected]/my-org/my-repo.git", anonAuth); |
| 79 | + |
| 80 | + var url2 = client.updateUrl("https://gitserver.local/my-org/my-repo.git", null); |
| 81 | + // auth added |
| 82 | + assertEquals( "https://[email protected]/my-org/my-repo.git", url2); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testGitHostRestriction() { |
| 88 | + var restrictedClient = new GitClient(GitClientConfiguration.builder() |
| 89 | + .from(cfg) |
| 90 | + .build(), RESTRICTED_AUTH_PROVIDER); |
| 91 | + |
| 92 | + var anonAuth = restrictedClient.updateUrl("https://elsewhere.local/my-org/my-repo.git", null); |
| 93 | + // unchanged |
| 94 | + assertEquals("https://elsewhere.local/my-org/my-repo.git", anonAuth); |
| 95 | + |
| 96 | + var url2 = restrictedClient.updateUrl("https://gitserver.local/my-org/my-repo.git", null); |
| 97 | + // auth added |
| 98 | + assertEquals( "https://[email protected]/my-org/my-repo.git", url2); |
| 99 | + } |
| 100 | + |
| 101 | + @Value.Immutable |
| 102 | + @Value.Style(jdkOnly = true) |
| 103 | + interface TestOauthTokenConfig extends com.walmartlabs.concord.common.cfg.OauthTokenConfig { |
| 104 | + static ImmutableTestOauthTokenConfig.Builder builder() { |
| 105 | + return ImmutableTestOauthTokenConfig.builder(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments