Search in sources :

Example 1 with Base64

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;
}
Also used : BuildService(io.fabric8.maven.core.service.BuildService) JsonPrimitive(com.google.gson.JsonPrimitive) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageName(io.fabric8.maven.docker.util.ImageName) RegistryService(io.fabric8.maven.docker.service.RegistryService)

Example 2 with Base64

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));
    }
}
Also used : Expectations(mockit.Expectations) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) Configuration(io.fabric8.maven.core.model.Configuration) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) Test(org.junit.Test)

Example 3 with Base64

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");
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) Secret(io.fabric8.kubernetes.api.model.Secret) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Test(org.junit.Test)

Example 4 with Base64

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");
}
Also used : SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) HashMap(java.util.HashMap) MockEnvironment(org.springframework.mock.env.MockEnvironment) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with Base64

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);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

Secret (io.fabric8.kubernetes.api.model.Secret)157 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)117 Test (org.junit.jupiter.api.Test)69 Base64 (java.util.Base64)61 Map (java.util.Map)55 List (java.util.List)40 HashMap (java.util.HashMap)39 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)38 Reconciliation (io.strimzi.operator.common.Reconciliation)36 CertManager (io.strimzi.certs.CertManager)32 VertxExtension (io.vertx.junit5.VertxExtension)32 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)32 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)31 StandardCharsets (java.nio.charset.StandardCharsets)29 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)28 Future (io.vertx.core.Future)28 BeforeAll (org.junit.jupiter.api.BeforeAll)27 KafkaUser (io.strimzi.api.kafka.model.KafkaUser)26 Labels (io.strimzi.operator.common.model.Labels)26 Vertx (io.vertx.core.Vertx)26