-
Notifications
You must be signed in to change notification settings - Fork 351
[Security/Extension] Role encryption/decryption #2620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RyanL1997
merged 9 commits into
opensearch-project:feature/extensions
from
RyanL1997:role-encryption
Apr 6, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
78e10d1
Encryption/Decryption of `roles`
RyanL1997 ae89338
Add negative test case for roles encryption
RyanL1997 3503fe4
fix spotless test
RyanL1997 a6fef8e
remove unused imports
RyanL1997 3ac533e
remove unused imports 2
RyanL1997 092751c
Upgrade spotbugs to version 5.0.14
RyanL1997 6fe6327
remove the version upgrade
RyanL1997 9694f80
Add test cases for bad roles
RyanL1997 bc14703
Add test with bad encryption_key and modify the bad role test case
RyanL1997 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtil.java
This file contains hidden or 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,55 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| * Modifications Copyright OpenSearch Contributors. See | ||
| * GitHub history for details. | ||
| */ | ||
|
|
||
| package org.opensearch.security.authtoken.jwt; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Base64; | ||
|
|
||
| import javax.crypto.Cipher; | ||
| import javax.crypto.SecretKey; | ||
| import javax.crypto.spec.SecretKeySpec; | ||
|
|
||
| public class EncryptionDecryptionUtil { | ||
|
|
||
| public static String encrypt(final String secret, final String data) { | ||
|
|
||
| byte[] decodedKey = Base64.getDecoder().decode(secret); | ||
|
|
||
| try { | ||
| Cipher cipher = Cipher.getInstance("AES"); | ||
| // rebuild key using SecretKeySpec | ||
| SecretKey originalKey = new SecretKeySpec(Arrays.copyOf(decodedKey, 16), "AES"); | ||
| cipher.init(Cipher.ENCRYPT_MODE, originalKey); | ||
| byte[] cipherText = cipher.doFinal(data.getBytes("UTF-8")); | ||
| return Base64.getEncoder().encodeToString(cipherText); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException( | ||
| "Error occured while encrypting data", e); | ||
| } | ||
| } | ||
|
|
||
| public static String decrypt(final String secret, final String encryptedString) { | ||
|
|
||
| byte[] decodedKey = Base64.getDecoder().decode(secret); | ||
|
|
||
| try { | ||
| Cipher cipher = Cipher.getInstance("AES"); | ||
| // rebuild key using SecretKeySpec | ||
| SecretKey originalKey = new SecretKeySpec(Arrays.copyOf(decodedKey, 16), "AES"); | ||
| cipher.init(Cipher.DECRYPT_MODE, originalKey); | ||
| byte[] cipherText = cipher.doFinal(Base64.getDecoder().decode(encryptedString)); | ||
| return new String(cipherText); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Error occured while decrypting data", e); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.