Skip to content

Commit

Permalink
Hide Hasicorp Vault from CLI (#9700)
Browse files Browse the repository at this point in the history
Closes #9688
  • Loading branch information
pedroigor committed Jan 25, 2022
1 parent 194c95d commit d28b54e
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
import org.keycloak.url.FixedHostnameProviderFactory;
import org.keycloak.url.RequestHostnameProviderFactory;
import org.keycloak.util.JsonSerialization;
import org.keycloak.vault.FilesPlainTextVaultProviderFactory;

class KeycloakProcessor {

Expand All @@ -146,7 +147,8 @@ class KeycloakProcessor {
LiquibaseJpaUpdaterProviderFactory.class,
DefaultHostnameProviderFactory.class,
FixedHostnameProviderFactory.class,
RequestHostnameProviderFactory.class);
RequestHostnameProviderFactory.class,
FilesPlainTextVaultProviderFactory.class);

static {
DEPLOYEABLE_SCRIPT_PROVIDERS.put(AUTHENTICATORS, KeycloakProcessor::registerScriptAuthenticator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,44 @@ private VaultPropertyMappers() {
public static PropertyMapper[] getVaultPropertyMappers() {
return new PropertyMapper[] {
builder()
.from("vault-file-path")
.to("kc.spi-vault-files-plaintext-dir")
.description("If set, secrets can be obtained by reading the content of files within the given path.")
.from("vault")
.description("Enables a vault provider.")
.expectedValues("file", "hashicorp")
.paramLabel("provider")
.isBuildTimeProperty(true)
.build(),
builder()
.from("vault-dir")
.to("kc.spi-vault-file-dir")
.description("If set, secrets can be obtained by reading the content of files within the given directory.")
.paramLabel("dir")
.build(),
builder()
.from("vault-hashicorp-")
.from("vault-")
.to("quarkus.vault.")
.description("If set, secrets can be obtained from Hashicorp Vault.")
.description("Maps any vault option to their corresponding properties in quarkus-vault extension.")
.hidden(true)
.isBuildTimeProperty(true)
.build(),
builder()
.from("vault-url")
.to("quarkus.vault.url")
.description("The vault server url.")
.paramLabel("paths")
.hidden(true)
.isBuildTimeProperty(true)
.build(),
builder()
.from("vault-hashicorp-paths")
.from("vault-kv-paths")
.to("kc.spi-vault-hashicorp-paths")
.description("A set of one or more paths that should be used when looking up secrets.")
.description("A set of one or more key/value paths that should be used when looking up secrets.")
.paramLabel("paths")
.hidden(true)
.build()
};
}

private static PropertyMapper.Builder builder() {
return PropertyMapper.builder(ConfigCategory.VAULT).isBuildTimeProperty(true);
return PropertyMapper.builder(ConfigCategory.VAULT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.keycloak.quarkus.runtime.vault;

import org.keycloak.Config;
import org.keycloak.provider.EnvironmentDependentProviderFactory;
import org.keycloak.quarkus.runtime.configuration.Configuration;

public class FilesPlainTextVaultProviderFactory extends org.keycloak.vault.FilesPlainTextVaultProviderFactory
implements EnvironmentDependentProviderFactory {

public static final String ID = "file";

@Override
public String getId() {
return ID;
}

@Override
public boolean isSupported() {
return false;
}

@Override
public boolean isSupported(Config.Scope config) {
return getId().equals(Configuration.getRawValue("kc.vault"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.provider.EnvironmentDependentProviderFactory;
import org.keycloak.quarkus.runtime.configuration.Configuration;
import org.keycloak.vault.AbstractVaultProviderFactory;
import org.keycloak.vault.VaultProvider;

Expand Down Expand Up @@ -77,7 +78,7 @@ public int order() {

@Override
public boolean isSupported(Config.Scope config) {
return !config.getPropertyNames().isEmpty();
return getId().equals(Configuration.getRawValue("kc.vault"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.keycloak.quarkus.runtime.vault.QuarkusVaultProviderFactory
org.keycloak.quarkus.runtime.vault.FilesPlainTextVaultProviderFactory
org.keycloak.quarkus.runtime.vault.QuarkusVaultProviderFactory
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.io.File;
import java.lang.reflect.Field;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
Expand All @@ -45,7 +44,7 @@
import io.quarkus.runtime.configuration.ConfigUtils;
import io.smallrye.config.SmallRyeConfigProviderResolver;
import org.keycloak.quarkus.runtime.Environment;
import org.keycloak.vault.FilesPlainTextVaultProviderFactory;
import org.keycloak.quarkus.runtime.vault.FilesPlainTextVaultProviderFactory;
import org.mariadb.jdbc.MySQLDataSource;
import org.postgresql.xa.PGXADataSource;

Expand Down Expand Up @@ -124,11 +123,11 @@ public void testEnvVarPriorityOverPropertiesFile() {

@Test
public void testEnvVarAvailableFromPropertyNames() {
putEnvVar("KC_VAULT_FILE_PATH", "/foo/bar");
Config.Scope config = initConfig("vault", FilesPlainTextVaultProviderFactory.PROVIDER_ID);
putEnvVar("KC_VAULT_DIR", "/foo/bar");
Config.Scope config = initConfig("vault", FilesPlainTextVaultProviderFactory.ID);
assertEquals("/foo/bar", config.get("dir"));
assertTrue(config.getPropertyNames()
.contains("kc.spi-vault-".concat(FilesPlainTextVaultProviderFactory.PROVIDER_ID).concat("-dir")));
.contains("kc.spi-vault-".concat(FilesPlainTextVaultProviderFactory.ID).concat("-dir")));
}

@Test
Expand Down Expand Up @@ -197,8 +196,8 @@ public void testPropertyNamesFromConfig() {
assertEquals(1, config.getPropertyNames().size());
assertEquals("http://c.jwk.url", config.get("static-jwk-url"));

System.setProperty(CLI_ARGS, "--vault-file-path=secrets");
config = initConfig("vault", FilesPlainTextVaultProviderFactory.PROVIDER_ID);
System.setProperty(CLI_ARGS, "--vault-dir=secrets");
config = initConfig("vault", FilesPlainTextVaultProviderFactory.ID);
assertEquals(1, config.getPropertyNames().size());
assertEquals("secrets", config.get("dir"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ Metrics:

Vault:

--vault-file-path <dir>
If set, secrets can be obtained by reading the content of files within the
given path.
--vault-hashicorp-paths <paths>
A set of one or more paths that should be used when looking up secrets.
--vault <provider> Enables a vault provider.

Examples:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ Proxy:
--proxy <mode> The proxy address forwarding mode if the server is behind a reverse proxy.
Possible values are: none,edge,reencrypt,passthrough Default: none.

Vault:

--vault-dir <dir> If set, secrets can be obtained by reading the content of files within the
given directory.

Do NOT start the server using this command when deploying to production.

Use 'kc.sh start-dev --help-all' to list all available options, including build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,9 @@ Proxy:

Vault:

--vault-file-path <dir>
If set, secrets can be obtained by reading the content of files within the
given path.
--vault-hashicorp-paths <paths>
A set of one or more paths that should be used when looking up secrets.
--vault <provider> Enables a vault provider.
--vault-dir <dir> If set, secrets can be obtained by reading the content of files within the
given directory.

Do NOT start the server using this command when deploying to production.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ Proxy:
--proxy <mode> The proxy address forwarding mode if the server is behind a reverse proxy.
Possible values are: none,edge,reencrypt,passthrough Default: none.

Vault:

--vault-dir <dir> If set, secrets can be obtained by reading the content of files within the
given directory.

You may use the "--auto-build" option when starting the server to avoid running
the "build" command everytime you need to change a static property:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ spi-events-store-jpa-max-detail-length=2000
spi-login-protocol-saml-known-protocols=http=8180,https=8543

# File-Based Vault
vault-file-path=${kc.home.dir}secrets
vault=file
vault-dir=${kc.home.dir}secrets

0 comments on commit d28b54e

Please sign in to comment.