|
41 | 41 | */ |
42 | 42 | public class RegistryAuthenticator { |
43 | 43 |
|
| 44 | + /** Initializer for {@link RegistryAuthenticator}. */ |
| 45 | + public static class Initializer { |
| 46 | + |
| 47 | + private final String serverUrl; |
| 48 | + private final String repository; |
| 49 | + private boolean allowHttp = false; |
| 50 | + |
| 51 | + /** |
| 52 | + * Instantiates a new initializer for {@link RegistryAuthenticator}. |
| 53 | + * |
| 54 | + * @param serverUrl the server URL for the registry (for example, {@code gcr.io}) |
| 55 | + * @param repository the image/repository name (also known as, namespace) |
| 56 | + */ |
| 57 | + private Initializer(String serverUrl, String repository) { |
| 58 | + this.serverUrl = serverUrl; |
| 59 | + this.repository = repository; |
| 60 | + } |
| 61 | + |
| 62 | + public Initializer setAllowHttp(boolean allowHttp) { |
| 63 | + this.allowHttp = allowHttp; |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Gets a {@link RegistryAuthenticator} for a custom registry server and repository. |
| 69 | + * |
| 70 | + * @return the {@link RegistryAuthenticator} to authenticate pulls/pushes with the registry, or |
| 71 | + * {@code null} if no token authentication is necessary |
| 72 | + * @throws RegistryAuthenticationFailedException if failed to create the registry authenticator |
| 73 | + * @throws IOException if communicating with the endpoint fails |
| 74 | + * @throws RegistryException if communicating with the endpoint fails |
| 75 | + */ |
| 76 | + @Nullable |
| 77 | + public RegistryAuthenticator initialize() |
| 78 | + throws RegistryAuthenticationFailedException, IOException, RegistryException { |
| 79 | + try { |
| 80 | + return RegistryClient.factory(serverUrl, repository) |
| 81 | + .setAllowHttp(allowHttp) |
| 82 | + .newRegistryClient() |
| 83 | + .getRegistryAuthenticator(); |
| 84 | + |
| 85 | + } catch (MalformedURLException ex) { |
| 86 | + throw new RegistryAuthenticationFailedException(ex); |
| 87 | + |
| 88 | + } catch (InsecureRegistryException ex) { |
| 89 | + // HTTP is not allowed, so just return null. |
| 90 | + return null; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Gets a new initializer for {@link RegistryAuthenticator}. |
| 97 | + * |
| 98 | + * @param serverUrl the server URL for the registry (for example, {@code gcr.io}) |
| 99 | + * @param repository the image/repository name (also known as, namespace) |
| 100 | + * @return the new {@link Initializer} |
| 101 | + */ |
| 102 | + public static Initializer initializer(String serverUrl, String repository) { |
| 103 | + return new Initializer(serverUrl, repository); |
| 104 | + } |
| 105 | + |
44 | 106 | // TODO: Replace with a WWW-Authenticate header parser. |
45 | 107 | /** |
46 | 108 | * Instantiates from parsing a {@code WWW-Authenticate} header. |
|
0 commit comments