-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support providing root client ID via env. variables when bootstrapping (
#422) * Support providing root client ID via env. variables for bootstrapping Introduce a `PrincipalSecretsGenerator` interface to isolate secrets generation from principal management code. Update meta store factories to allow the user to define the root client ID and secret via environment variables during bootstrapping. * review: add @NotNull --------- Co-authored-by: Eric Maynard <[email protected]>
- Loading branch information
1 parent
769424f
commit 29a9828
Showing
12 changed files
with
200 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ris-core/src/main/java/org/apache/polaris/core/persistence/PrincipalSecretsGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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.apache.polaris.core.persistence; | ||
|
||
import java.util.Locale; | ||
import java.util.function.Function; | ||
import org.apache.polaris.core.entity.PolarisPrincipalSecrets; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* An interface for generating principal secrets. It enables detaching the secret generation logic | ||
* from services that actually manage principal objects (create, remove, rotate secrets, etc.) | ||
* | ||
* <p>The implementation statically available from {@link #bootstrap(String)} allows one-time client | ||
* ID and secret overrides via environment variables, which can be useful for bootstrapping new | ||
* realms. | ||
* | ||
* <p>The environment variable name follow this pattern: | ||
* | ||
* <ul> | ||
* <li>{@code POLARIS_BOOTSTRAP_<REALM-NAME>_<PRINCIPAL-NAME>_CLIENT_ID} | ||
* <li>{@code POLARIS_BOOTSTRAP_<REALM-NAME>_<PRINCIPAL-NAME>_CLIENT_SECRET} | ||
* </ul> | ||
* | ||
* For example: {@code POLARIS_BOOTSTRAP_DEFAULT-REALM_ROOT_CLIENT_ID} and {@code | ||
* POLARIS_BOOTSTRAP_DEFAULT-REALM_ROOT_CLIENT_SECRET}. | ||
*/ | ||
@FunctionalInterface | ||
public interface PrincipalSecretsGenerator { | ||
|
||
/** | ||
* A secret generator that produces cryptographically random client ID and client secret values. | ||
*/ | ||
PrincipalSecretsGenerator RANDOM_SECRETS = (name, id) -> new PolarisPrincipalSecrets(id); | ||
|
||
/** | ||
* Produces a new {@link PolarisPrincipalSecrets} object for the given principal ID. The returned | ||
* secrets may or may not be random, depending on context. In bootstrapping contexts, the returned | ||
* secrets can be predefined. After bootstrapping, the returned secrets can be expected to be | ||
* cryptographically random. | ||
* | ||
* @param principalName the name of the related principal. This parameter is a hint for | ||
* pre-defined secrets lookup during bootstrapping it is not included in the returned data. | ||
* @param principalId the ID of the related principal. This ID is part of the returned data. | ||
* @return a new {@link PolarisPrincipalSecrets} instance for the specified principal. | ||
*/ | ||
PolarisPrincipalSecrets produceSecrets(@NotNull String principalName, long principalId); | ||
|
||
static PrincipalSecretsGenerator bootstrap(String realmName) { | ||
return bootstrap(realmName, System.getenv()::get); | ||
} | ||
|
||
static PrincipalSecretsGenerator bootstrap(String realmName, Function<String, String> config) { | ||
return (principalName, principalId) -> { | ||
String propId = String.format("POLARIS_BOOTSTRAP_%s_%s_CLIENT_ID", realmName, principalName); | ||
String propSecret = | ||
String.format("POLARIS_BOOTSTRAP_%s_%s_CLIENT_SECRET", realmName, principalName); | ||
|
||
String clientId = config.apply(propId.toUpperCase(Locale.ROOT)); | ||
String secret = config.apply(propSecret.toUpperCase(Locale.ROOT)); | ||
// use config values at most once (do not interfere with secret rotation) | ||
if (clientId != null && secret != null) { | ||
return new PolarisPrincipalSecrets(principalId, clientId, secret, secret); | ||
} else { | ||
return RANDOM_SECRETS.produceSecrets(principalName, principalId); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...core/src/test/java/org/apache/polaris/core/persistence/PrincipalSecretsGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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.apache.polaris.core.persistence; | ||
|
||
import static org.apache.polaris.core.persistence.PrincipalSecretsGenerator.bootstrap; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Map; | ||
import org.apache.polaris.core.entity.PolarisPrincipalSecrets; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class PrincipalSecretsGeneratorTest { | ||
|
||
@Test | ||
void testRandomSecrets() { | ||
PolarisPrincipalSecrets s = bootstrap("test", (name) -> null).produceSecrets("name1", 123); | ||
assertThat(s).isNotNull(); | ||
assertThat(s.getPrincipalId()).isEqualTo(123); | ||
assertThat(s.getPrincipalClientId()).isNotNull(); | ||
assertThat(s.getMainSecret()).isNotNull(); | ||
assertThat(s.getSecondarySecret()).isNotNull(); | ||
} | ||
|
||
@Test | ||
void testSecretOverride() { | ||
PrincipalSecretsGenerator gen = | ||
bootstrap( | ||
"test-Realm", | ||
Map.of( | ||
"POLARIS_BOOTSTRAP_TEST-REALM_USER1_CLIENT_ID", | ||
"client1", | ||
"POLARIS_BOOTSTRAP_TEST-REALM_USER1_CLIENT_SECRET", | ||
"sec2") | ||
::get); | ||
PolarisPrincipalSecrets s = gen.produceSecrets("user1", 123); | ||
assertThat(s).isNotNull(); | ||
assertThat(s.getPrincipalId()).isEqualTo(123); | ||
assertThat(s.getPrincipalClientId()).isEqualTo("client1"); | ||
assertThat(s.getMainSecret()).isEqualTo("sec2"); | ||
assertThat(s.getSecondarySecret()).isEqualTo("sec2"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters