-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ELY-2078] Add encryption support to FileSystemSecurityRealm
- Loading branch information
Showing
9 changed files
with
1,563 additions
and
68 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
51 changes: 51 additions & 0 deletions
51
auth/realm/base/src/main/java/org/wildfly/security/auth/realm/FileSystemRealmUtil.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,51 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2021 Red Hat, Inc., and individual 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.wildfly.security.auth.realm; | ||
|
||
|
||
import java.util.List; | ||
import org.wildfly.security.auth.principal.NamePrincipal; | ||
import org.wildfly.security.auth.server.ModifiableRealmIdentity; | ||
import org.wildfly.security.auth.server.ModifiableRealmIdentityIterator; | ||
import org.wildfly.security.auth.server.RealmUnavailableException; | ||
import org.wildfly.security.authz.Attributes; | ||
import org.wildfly.security.credential.Credential; | ||
|
||
/** | ||
* A utility class to utilize methods from the {@code FileSystemSecurityRealm} class for the Elytron Tool. | ||
* | ||
* @author <a href="mailto:[email protected]">Ashpan Raskar</a> | ||
*/ | ||
public class FileSystemRealmUtil { | ||
public static void createEncryptedRealmFromUnencrypted(FileSystemSecurityRealm unencryptedRealm, FileSystemSecurityRealm encryptedRealm) throws RealmUnavailableException { | ||
ModifiableRealmIdentityIterator realmIterator = unencryptedRealm.getRealmIdentityIterator(); | ||
|
||
while (realmIterator.hasNext()) { | ||
ModifiableRealmIdentity identity = realmIterator.next(); | ||
List<Credential> credentials = ((FileSystemSecurityRealm.Identity) identity).loadCredentials(); | ||
Attributes attributes = identity.getAttributes(); | ||
|
||
ModifiableRealmIdentity newIdentity = encryptedRealm.getRealmIdentityForUpdate(new NamePrincipal(identity.getRealmIdentityPrincipal().getName())); | ||
newIdentity.create(); | ||
newIdentity.setCredentials(credentials); | ||
newIdentity.setAttributes(attributes); | ||
} | ||
realmIterator.close(); | ||
} | ||
|
||
} |
138 changes: 99 additions & 39 deletions
138
auth/realm/base/src/main/java/org/wildfly/security/auth/realm/FileSystemSecurityRealm.java
Large diffs are not rendered by default.
Oops, something went wrong.
158 changes: 158 additions & 0 deletions
158
...lm/base/src/main/java/org/wildfly/security/auth/realm/FileSystemSecurityRealmBuilder.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,158 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2021 Red Hat, Inc., and individual 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.wildfly.security.auth.realm; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Path; | ||
import java.security.Provider; | ||
import java.util.function.Supplier; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
import org.wildfly.common.Assert; | ||
import org.wildfly.security.auth.server.NameRewriter; | ||
import org.wildfly.security.password.spec.Encoding; | ||
|
||
|
||
/** | ||
* A builder class that creates {@link FileSystemSecurityRealm} instances. | ||
* | ||
* @author <a href="mailto:[email protected]">Ashpan Raskar</a> | ||
*/ | ||
public class FileSystemSecurityRealmBuilder { | ||
|
||
private Path root; | ||
private NameRewriter nameRewriter; | ||
private int levels = 2; | ||
private boolean encoded = true; | ||
private Charset hashCharset; | ||
private Encoding hashEncoding; | ||
private SecretKey secretKey; | ||
private Supplier<Provider[]> providers; | ||
|
||
FileSystemSecurityRealmBuilder() { | ||
} | ||
|
||
/** | ||
* Set the root path to be used by the realm. | ||
* | ||
* @param root the root path of the identity store (must not be {@code null}) | ||
* @return this builder.enc | ||
*/ | ||
public FileSystemSecurityRealmBuilder setRoot(final Path root) { | ||
Assert.checkNotNullParam("root", root); | ||
this.root = root; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the name rewriter to be used by the realm. | ||
* | ||
* @param nameRewriter the name rewriter to apply to looked up names (must not be {@code null}) | ||
* @return this builder. | ||
*/ | ||
public FileSystemSecurityRealmBuilder setNameRewriter(final NameRewriter nameRewriter) { | ||
Assert.checkNotNullParam("nameRewriter", nameRewriter); | ||
this.nameRewriter = nameRewriter; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the number of levels to be used by the realm. | ||
* | ||
* @param levels the number of levels of directory hashing to apply (must not be {@code null}) | ||
* @return this builder. | ||
*/ | ||
public FileSystemSecurityRealmBuilder setLevels(final int levels) { | ||
this.levels = levels; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set whether the identity name should be encoded for the filename in the realm. | ||
* | ||
* @param encoded whether identity names should be BASE32 encoded before using as filename (only applies if the security realm is unencrypted) | ||
* @return this builder. | ||
*/ | ||
public FileSystemSecurityRealmBuilder setEncoded(final boolean encoded) { | ||
this.encoded = encoded; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the character set to be used by the realm. | ||
* | ||
* @param hashCharset the character set to use when converting password strings to a byte array. Uses UTF-8 by default. (must not be {@code null}) | ||
* @return this builder. | ||
*/ | ||
public FileSystemSecurityRealmBuilder setHashCharset(final Charset hashCharset) { | ||
Assert.checkNotNullParam("hashCharset", hashCharset); | ||
this.hashCharset = hashCharset; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the string format for hashed passwords to be used by the realm. | ||
* | ||
* @param hashEncoding the string format for the hashed passwords. Uses Base64 by default. (must not be {@code null}) | ||
* @return this builder. | ||
*/ | ||
public FileSystemSecurityRealmBuilder setHashEncoding(final Encoding hashEncoding) { | ||
Assert.checkNotNullParam("hashEncoding", hashEncoding); | ||
this.hashEncoding = hashEncoding; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the SecretKey to be used by the realm. | ||
* | ||
* @param secretKey the symmetric SecretKey used to encrypt and decrypt the Security Realm (must not be {@code null}) | ||
* @return this builder. | ||
*/ | ||
public FileSystemSecurityRealmBuilder setSecretKey(final SecretKey secretKey) { | ||
Assert.checkNotNullParam("secretKey", secretKey); | ||
this.secretKey = secretKey; | ||
return this; | ||
} | ||
|
||
public FileSystemSecurityRealmBuilder setProviders(final Supplier<Provider[]> providers) { | ||
Assert.checkNotNullParam("providers", providers); | ||
this.providers = providers; | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds a new {@link FileSystemSecurityRealm} instance based on configuration defined for this {@link FileSystemSecurityRealmBuilder} instance. | ||
* | ||
* @return the built realm | ||
*/ | ||
public FileSystemSecurityRealm build() { | ||
encoded = secretKey == null && encoded; | ||
if (nameRewriter == null) { | ||
nameRewriter = NameRewriter.IDENTITY_REWRITER; | ||
} | ||
if (hashEncoding == null) { | ||
hashEncoding = Encoding.BASE64; | ||
} | ||
if (hashCharset == null) { | ||
hashCharset = StandardCharsets.UTF_8; | ||
} | ||
return new FileSystemSecurityRealm(root, nameRewriter, levels, encoded, hashEncoding, hashCharset, providers, secretKey); | ||
} | ||
} |
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
Oops, something went wrong.