Skip to content

Commit

Permalink
secret providers: fixing handling of encrypted/external data in prote…
Browse files Browse the repository at this point in the history
…cted data type
  • Loading branch information
1azyman committed Feb 23, 2024
1 parent d012cd3 commit 8d39929
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public interface ProtectedData<T> {

boolean isEncrypted();

boolean isExternal();

HashedDataType getHashedDataType();

void setHashedData(HashedDataType hashedDataType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public boolean isEncrypted() {
return encryptedDataType != null;
}

@Override
public boolean isExternal() {
return externalDataType != null;
}

@Override
public HashedDataType getHashedDataType() {
return hashedDataType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ public abstract class BaseProtector implements Protector {

@Override
public <T> void decrypt(ProtectedData<T> protectedData) throws EncryptionException, SchemaException {
if (!protectedData.isEncrypted()) {
return;
//TODO: is this exception really needed?? isn't it better just return the same protected data??
// throw new IllegalArgumentException("Attempt to decrypt protected data that are not encrypted");
} else {
if (protectedData.isEncrypted()) {
byte[] decryptedData = decryptBytes(protectedData);
protectedData.setClearBytes(decryptedData);
protectedData.setEncryptedData(null);
} else if (protectedData.isExternal()) {
throw new EncryptionException("This protector implementation can't resolve external data");
}
}

Expand All @@ -36,13 +34,15 @@ public <T> void decrypt(ProtectedData<T> protectedData) throws EncryptionExcepti
@Override
public String decryptString(ProtectedData<String> protectedString) throws EncryptionException {
try {
if (!protectedString.isEncrypted()) {
return protectedString.getClearValue();
} else {
if (protectedString.isEncrypted()) {
byte[] clearBytes = decryptBytes(protectedString);
return ProtectedStringType.bytesToString(clearBytes);
} else if (protectedString.isExternal()) {
throw new EncryptionException("This protector implementation can't resolve external data");
}
} catch (SchemaException ex){

return protectedString.getClearValue();
} catch (SchemaException ex) {
throw new EncryptionException(ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ protected <T> byte[] decryptBytes(ProtectedData<T> protectedData)

@Override
public <T> void encrypt(ProtectedData<T> protectedData) throws EncryptionException {
if (protectedData.isExternal()) {
protectedData.destroyCleartext();
return;
}
if (protectedData.isEncrypted()) {
throw new IllegalArgumentException(
"Attempt to encrypt protected data that are already encrypted");
Expand Down Expand Up @@ -582,6 +586,8 @@ private HashedDataType hashPbkd(
private char[] getClearChars(ProtectedData<String> protectedData) throws EncryptionException {
if (protectedData.isEncrypted()) {
return decryptString(protectedData).toCharArray();
} else if (protectedData.isExternal()) {
throw new EncryptionException("This protector implementation can't resolve external data");
} else {
return protectedData.getClearValue().toCharArray();
}
Expand Down Expand Up @@ -709,6 +715,13 @@ public boolean areEquivalent(ProtectedStringType a, ProtectedStringType b) {
return false;
}
}
if (a.isExternal()) {
if (b.isExternal()) {
return areEquivalentExternal(a,b);
} else {
return false;
}
}
return Objects.equals(a.getClearValue(), b.getClearValue());
}

Expand All @@ -717,6 +730,13 @@ private boolean areEquivalentHashed(ProtectedStringType a, ProtectedStringType b
return Objects.equals(a.getHashedDataType(), b.getHashedDataType());
}

private boolean areEquivalentExternal(ProtectedStringType a, ProtectedStringType b) {
ExternalDataType ae = a.getExternalData();
ExternalDataType be = b.getExternalData();

return Objects.equals(ae, be);
}

private boolean areEquivalentEncrypted(ProtectedStringType a, ProtectedStringType b) {
EncryptedDataType ae = a.getEncryptedDataType();
EncryptedDataType be = b.getEncryptedDataType();
Expand Down

0 comments on commit 8d39929

Please sign in to comment.