-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example illustrating how to query credential store created from C…
…LI in the java code
- Loading branch information
Showing
3 changed files
with
179 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Query existing credential store from code | ||
============================================================= | ||
|
||
This example demonstrates how to programmatically query credential store that was created with WildFly CLI. | ||
|
||
Add a credential store with aliases via WildFly CLI: | ||
|
||
```shell | ||
/subsystem=elytron/credential-store=my_credential_store:add(location="PATH/TO/credential-store.cs", credential-reference={clear-text=StorePassword},create=true) | ||
{"outcome" => "success"} | ||
/subsystem=elytron/credential-store=my_credential_store:add-alias(alias=my-secret-db-password, secret-value="db-secret") | ||
{"outcome" => "success"} | ||
/subsystem=elytron/credential-store=my_credential_store:add-alias(alias=my-secret-access-password, secret-value="access-pw") | ||
{"outcome" => "success"} | ||
``` | ||
Credential Store has been created and 2 aliases `my-secret-access-password` and `my-secret-db-password` have been added. | ||
|
||
Look at `ExistingCredentialStoreQueryExample` class to see how this credential store can be queried from code. For creation and population of credential store from code, please see `credential-store` example in this repository. | ||
|
||
Run this command to execute this class: | ||
```shell | ||
$ mvn clean install exec:exec | ||
``` | ||
|
||
Expected output: | ||
``` | ||
************************************ | ||
Current Aliases in credential store: | ||
- my-secret-db-password | ||
- my-secret-access-password | ||
************************************ | ||
Your secret key for alias my-secret-db-password is: db-secret | ||
``` |
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,68 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.wildfly.security.examples</groupId> | ||
<artifactId>existing-credential-store-query</artifactId> | ||
<version>2.0.0.Alpha1-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
|
||
<version.wildfly>27.0.1.Final</version.wildfly> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.wildfly.bom</groupId> | ||
<artifactId>wildfly-ee</artifactId> | ||
<scope>import</scope> | ||
<type>pom</type> | ||
<version>${version.wildfly}</version> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.wildfly.security</groupId> | ||
<artifactId>wildfly-elytron-credential-store</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.wildfly.security</groupId> | ||
<artifactId>wildfly-elytron-password-impl</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<!-- Add the Maven exec plug-in to allow us to run a Java program via Maven --> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<configuration> | ||
<executable>java</executable> | ||
<workingDirectory>${project.build.directory}/exec-working-directory</workingDirectory> | ||
<arguments> | ||
<!-- automatically creates the classpath using all project dependencies, | ||
also adding the project build directory --> | ||
<argument>-classpath</argument> | ||
<classpath></classpath> | ||
<argument>org.wildfly.security.examples.ExistingCredentialStoreQueryExample</argument> | ||
</arguments> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>exec</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
78 changes: 78 additions & 0 deletions
78
...uery/src/main/java/org/wildfly/security/examples/ExistingCredentialStoreQueryExample.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,78 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. | ||
* | ||
* 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.wildfly.security.examples; | ||
|
||
import java.security.Provider; | ||
import java.security.Security; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.wildfly.security.auth.server.IdentityCredentials; | ||
import org.wildfly.security.credential.PasswordCredential; | ||
import org.wildfly.security.credential.store.CredentialStore; | ||
import org.wildfly.security.credential.store.CredentialStore.CredentialSourceProtectionParameter; | ||
import org.wildfly.security.credential.store.CredentialStore.ProtectionParameter; | ||
import org.wildfly.security.credential.store.WildFlyElytronCredentialStoreProvider; | ||
import org.wildfly.security.password.Password; | ||
import org.wildfly.security.password.WildFlyElytronPasswordProvider; | ||
import org.wildfly.security.password.interfaces.ClearPassword; | ||
|
||
/** | ||
* Example demonstrating how a credential store created by WildFly CLI can be queried. | ||
*/ | ||
public class ExistingCredentialStoreQueryExample { | ||
|
||
private static final Provider CREDENTIAL_STORE_PROVIDER = new WildFlyElytronCredentialStoreProvider(); | ||
private static final Provider PASSWORD_PROVIDER = new WildFlyElytronPasswordProvider(); | ||
|
||
static { | ||
Security.addProvider(PASSWORD_PROVIDER); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
/* | ||
* Create a ProtectionParameter for access to the store. | ||
*/ | ||
Password storePassword = ClearPassword.createRaw(ClearPassword.ALGORITHM_CLEAR, "StorePassword".toCharArray()); | ||
ProtectionParameter protectionParameter = new CredentialSourceProtectionParameter(IdentityCredentials.NONE.withCredential(new PasswordCredential(storePassword))); | ||
// Get an instance of the CredentialStore | ||
CredentialStore credentialStore = CredentialStore.getInstance("KeyStoreCredentialStore", CREDENTIAL_STORE_PROVIDER); | ||
|
||
// Initialise the CredentialStore | ||
Map<String, String> configuration = new HashMap<>(); | ||
configuration.put("location", "/PATH/TO/credential-store.cs"); | ||
configuration.put("modifiable", "true"); | ||
configuration.put("keyStoreType", "JCEKS"); | ||
credentialStore.initialize(configuration, protectionParameter); | ||
|
||
System.out.println("************************************"); | ||
System.out.println("Current Aliases in credential store: "); | ||
for (String alias : credentialStore.getAliases()) { | ||
System.out.print(" - "); | ||
System.out.println(alias); | ||
} | ||
System.out.println("************************************"); | ||
|
||
String queriedAlias = "my-secret-db-password"; | ||
final PasswordCredential credential = credentialStore.retrieve(queriedAlias,PasswordCredential.class); | ||
final Password password = credential.castAndApply(PasswordCredential.class, ClearPassword.ALGORITHM_CLEAR, PasswordCredential::getPassword); | ||
final ClearPassword clearPassword = password.castAs(ClearPassword.class, ClearPassword.ALGORITHM_CLEAR); | ||
System.out.println("Your secret key for alias " + queriedAlias + " is: " + new String(clearPassword.getPassword())); | ||
} | ||
|
||
} |