GitCredentialStorageFileSecretApplier extends FileSecretApplier { @Override public void applySecret(KubernetesEnvironment env, RuntimeIdentity runtimeIdentity, Secret secret) throws InfrastructureException { super.applySecret(env, runtimeIdentity, secret); final String secretMountPath = secret.getMetadata().getAnnotations().get(ANNOTATION_MOUNT_PATH); Set<String> keys = secret.getData().keySet(); if (keys.size() != 1) { throw new InfrastructureException( format( "Invalid git credential secret data. It should contain only 1 data item but it have %d", keys.size())); } Path gitSecretFilePath = Paths.get(secretMountPath, keys.iterator().next()); ConfigMap gitConfigMap = env.getConfigMaps() .get( runtimeIdentity.getWorkspaceId() + GitConfigProvisioner.GIT_CONFIG_MAP_NAME_SUFFIX); if (gitConfigMap != null) { Map<String, String> gitConfigMapData = gitConfigMap.getData(); String gitConfig = gitConfigMapData.get(GitConfigProvisioner.GIT_CONFIG); if (gitConfig != null) { if (gitConfig.contains("helper = store --file") && gitConfig.contains("[credential]")) { throw new InfrastructureException( "Multiple git credentials secrets found. Please remove duplication."); } HashMap<String, String> newGitConfigMapData = new HashMap<>(gitConfigMapData); newGitConfigMapData.put( GitConfigProvisioner.GIT_CONFIG, gitConfig + String.format(GIT_CREDENTIALS_FILE_STORE_PATTERN, gitSecretFilePath.toString())); gitConfigMap.setData(newGitConfigMapData); } } } @Inject  GitCredentialStorageFileSecretApplier(K8sVersion k8sVersion); @Override void applySecret(KubernetesEnvironment env, RuntimeIdentity runtimeIdentity, Secret secret); static final String ANNOTATION_GIT_CREDENTIALS; }
@Test( expectedExceptions = InfrastructureException.class, expectedExceptionsMessageRegExp = "Invalid git credential secret data. It should contain only 1 data item but it have 2") public void shouldThrowInfrastructureExceptionIfSecretsHasMoreOrLessWhen1Data() throws InfrastructureException { Secret secret = new SecretBuilder() .withData(ImmutableMap.of("credentials", "random", "credentials2", "random")) .withMetadata( new ObjectMetaBuilder() .withName("test_secret") .withAnnotations( ImmutableMap.of( ANNOTATION_MOUNT_AS, "file", ANNOTATION_MOUNT_PATH, "/home/user/.git", GitCredentialStorageFileSecretApplier.ANNOTATION_GIT_CREDENTIALS, "true", ANNOTATION_AUTOMOUNT, "true")) .withLabels(emptyMap()) .build()) .build(); secretApplier.applySecret(environment, runtimeIdentity, secret); }
@Test public void shouldBeAbleToAdjustGiConfigConfigMap() throws InfrastructureException { Secret secret = new SecretBuilder() .withData(ImmutableMap.of("credentials", "random")) .withMetadata( new ObjectMetaBuilder() .withName("test_secret") .withAnnotations( ImmutableMap.of( ANNOTATION_MOUNT_AS, "file", ANNOTATION_MOUNT_PATH, "/home/user/.git", GitCredentialStorageFileSecretApplier.ANNOTATION_GIT_CREDENTIALS, "true", ANNOTATION_AUTOMOUNT, "true")) .withLabels(emptyMap()) .build()) .build(); ConfigMap configMap = new ConfigMapBuilder() .withData(ImmutableMap.of(GitConfigProvisioner.GIT_CONFIG, GIT_CONFIG_CONTENT)) .build(); when(environment.getConfigMaps()) .thenReturn( ImmutableMap.of( "ws-1234598" + GitConfigProvisioner.GIT_CONFIG_MAP_NAME_SUFFIX, configMap)); secretApplier.applySecret(environment, runtimeIdentity, secret); String data = configMap.getData().get(GitConfigProvisioner.GIT_CONFIG); assertTrue( data.endsWith("[credential]\n\thelper = store --file /home/user/.git/credentials\n")); assertTrue(data.startsWith(GIT_CONFIG_CONTENT)); }
@Test( expectedExceptions = InfrastructureException.class, expectedExceptionsMessageRegExp = "Multiple git credentials secrets found. Please remove duplication.") public void shouldThrowInfrastructureExceptionIfGitConfigAlreadyContainsSecretConfig() throws InfrastructureException { Secret secret = new SecretBuilder() .withData(ImmutableMap.of("credentials", "random")) .withMetadata( new ObjectMetaBuilder() .withName("test_secret") .withAnnotations( ImmutableMap.of( ANNOTATION_MOUNT_AS, "file", ANNOTATION_MOUNT_PATH, "/home/user/.git", GitCredentialStorageFileSecretApplier.ANNOTATION_GIT_CREDENTIALS, "true", ANNOTATION_AUTOMOUNT, "true")) .withLabels(emptyMap()) .build()) .build(); ConfigMap configMap = new ConfigMapBuilder() .withData( ImmutableMap.of( GitConfigProvisioner.GIT_CONFIG, GIT_CONFIG_CONTENT + "[credential]\n\thelper = store --file /home/user/.git/credentials\n")) .build(); when(environment.getConfigMaps()) .thenReturn( ImmutableMap.of( "ws-1234598" + GitConfigProvisioner.GIT_CONFIG_MAP_NAME_SUFFIX, configMap)); secretApplier.applySecret(environment, runtimeIdentity, secret); }