use of io.fabric8.kubernetes.client.utils.internal.Base64 in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildService method checkOrCreatePullSecret.
private Boolean checkOrCreatePullSecret(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder, String pullSecretName, ImageConfiguration imageConfig) throws MojoExecutionException, UnsupportedEncodingException {
io.fabric8.maven.docker.service.BuildService.BuildContext dockerBuildContext = config.getDockerBuildContext();
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
String fromImage;
if (buildConfig.isDockerFileMode()) {
fromImage = extractBaseFromDockerfile(buildConfig, dockerBuildContext);
} else {
fromImage = extractBaseFromConfiguration(buildConfig);
}
String pullRegistry = EnvUtil.firstRegistryOf(new ImageName(fromImage).getRegistry(), dockerBuildContext.getRegistryConfig().getRegistry(), dockerBuildContext.getRegistryConfig().getRegistry());
if (pullRegistry != null) {
RegistryService.RegistryConfig registryConfig = dockerBuildContext.getRegistryConfig();
AuthConfig authConfig = registryConfig.getAuthConfigFactory().createAuthConfig(false, registryConfig.isSkipExtendedAuth(), registryConfig.getAuthConfig(), registryConfig.getSettings(), null, pullRegistry);
if (authConfig != null) {
JsonObject auths = new JsonObject();
JsonObject auth = new JsonObject();
JsonObject item = new JsonObject();
String authString = authConfig.getUsername() + ":" + authConfig.getPassword();
item.add("auth", new JsonPrimitive(Base64.encodeBase64String(authString.getBytes("UTF-8"))));
auth.add(pullRegistry, item);
auths.add("auths", auth);
String credentials = Base64.encodeBase64String(auths.toString().getBytes("UTF-8"));
Map<String, String> data = new HashMap<>();
data.put(".dockerconfigjson", credentials);
boolean hasPullSecret = client.secrets().withName(pullSecretName).get() != null;
if (!hasPullSecret) {
log.info("Creating Secret %s", hasPullSecret);
builder.addNewSecretItem().withNewMetadata().withName(pullSecretName).endMetadata().withData(data).withType("kubernetes.io/dockerconfigjson").endSecretItem();
} else {
log.info("Adding to Secret %s", pullSecretName);
return updateSecret(client, pullSecretName, data);
}
return true;
} else {
return false;
}
}
return false;
}
use of io.fabric8.kubernetes.client.utils.internal.Base64 in project fabric8-maven-plugin by fabric8io.
the class ConfigMapEnricherTest method should_materialize_file_content_from_xml.
@Test
public void should_materialize_file_content_from_xml() throws Exception {
final io.fabric8.maven.core.config.ConfigMap baseConfigMap = createXmlConfigMap(file);
final ResourceConfig config = new ResourceConfig.Builder().withConfigMap(baseConfigMap).build();
new Expectations() {
{
context.getConfiguration();
result = new Configuration.Builder().resource(config).build();
context.resolvePath(file.toString());
result = file;
}
};
final KubernetesListBuilder builder = new KubernetesListBuilder();
new ConfigMapEnricher(context).create(PlatformMode.kubernetes, builder);
final ConfigMap configMap = (ConfigMap) builder.buildFirstItem();
if (binary) {
final Map<String, String> binaryData = configMap.getBinaryData();
assertThat(binaryData).containsEntry(file.getFileName().toString(), Base64.getEncoder().encodeToString(readFileContentAsBytes(file)));
} else {
final Map<String, String> data = configMap.getData();
assertThat(data).containsEntry(file.getFileName().toString(), readFileContentsAsString(file));
}
}
use of io.fabric8.kubernetes.client.utils.internal.Base64 in project fabric8-maven-plugin by fabric8io.
the class DockerRegistrySecretEnricherTest method testDockerRegistry.
@Test
public void testDockerRegistry() {
setupExpectations();
DockerRegistrySecretEnricher enricher = new DockerRegistrySecretEnricher(context);
KubernetesListBuilder builder = new KubernetesListBuilder();
Secret secretEnriched = createBaseSecret(true);
builder.addToSecretItems(secretEnriched);
enricher.create(PlatformMode.kubernetes, builder);
secretEnriched = (Secret) builder.buildItem(0);
Map<String, String> enrichedData = secretEnriched.getData();
assertThat(enrichedData.size()).isEqualTo(1);
String data = enrichedData.get(SecretConstants.DOCKER_DATA_KEY);
assertThat(data).isNotNull();
JsonObject auths = (JsonObject) new JsonParser().parse(new String(Base64.decodeBase64(data)));
assertThat(auths.size()).isEqualTo(1);
JsonObject auth = auths.getAsJsonObject("docker.io");
assertThat(auth.size()).isEqualTo(2);
assertThat(auth.get("username").getAsString()).isEqualTo("username");
assertThat(auth.get("password").getAsString()).isEqualTo("password");
}
use of io.fabric8.kubernetes.client.utils.internal.Base64 in project spring-cloud-kubernetes by spring-cloud.
the class SecretsRetryEnabled method locateShouldNotRetryWhenThereIsNoFailure.
@Test
void locateShouldNotRetryWhenThereIsNoFailure() {
Map<String, String> data = new HashMap<>();
data.put("some.sensitive.prop", Base64.getEncoder().encodeToString("theSensitiveValue".getBytes()));
data.put("some.sensitive.number", Base64.getEncoder().encodeToString("1".getBytes()));
// return secret without failing
mockServer.expect().withPath(API).andReturn(200, new SecretBuilder().withNewMetadata().withName("my-secret").endMetadata().addToData(data).build()).once();
PropertySource<?> propertySource = Assertions.assertDoesNotThrow(() -> propertySourceLocator.locate(new MockEnvironment()));
// verify locate is called only once
verify(propertySourceLocator, times(1)).locate(any());
// validate the contents of the property source
assertThat(propertySource.getProperty("some.sensitive.prop")).isEqualTo("theSensitiveValue");
assertThat(propertySource.getProperty("some.sensitive.number")).isEqualTo("1");
}
use of io.fabric8.kubernetes.client.utils.internal.Base64 in project spring-cloud-kubernetes by spring-cloud.
the class Fabric8SecretsPropertySourceTest method setUpBeforeClass.
@BeforeAll
static void setUpBeforeClass() {
// Configure the kubernetes master url to point to the mock server
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl());
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, NAMESPACE);
System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true");
Secret secret = new SecretBuilder().withNewMetadata().withName("test-secret").withLabels(singletonMap("foo", "bar")).endMetadata().addToData("secretName", Base64.getEncoder().encodeToString(SECRET_VALUE.getBytes())).build();
mockClient.secrets().inNamespace(NAMESPACE).create(secret);
}
Aggregations