Skip to content
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

Show masked value for secret metadata #3880

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions api/src/main/java/com/epam/pipeline/dao/metadata/MetadataDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class MetadataDao extends NamedParameterJdbcDaoSupport {
private static final String KEY = "KEY";
private static final String VALUE = "VALUE";
private static final String LIST_PARAMETER = "list";
public static final String SECRET_MASK_VALUE = "***";

private Pattern dataKeyPattern = Pattern.compile("@KEY@");
private Pattern entitiesValuePattern = Pattern.compile("@ENTITIES@");
Expand Down Expand Up @@ -398,11 +399,22 @@ public static Map<String, PipeConfValue> parseData(final String data) {
public static Map<String, PipeConfValue> parseData(final String data, final List<String> keysToRetrieve) {
final Map<String, PipeConfValue> parsedData = JsonMapper.parseData(
data, new TypeReference<Map<String, PipeConfValue>>() {});

if (CollectionUtils.isEmpty(parsedData)) {
return parsedData;
}

if (CollectionUtils.isEmpty(keysToRetrieve)) {
return parsedData.entrySet().stream()
.filter(metadataEntry ->
!SECRET_METADATA_TYPE.equalsIgnoreCase(metadataEntry.getValue().getType()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
.map(metadataEntry -> {
if (SECRET_METADATA_TYPE.equalsIgnoreCase(metadataEntry.getValue().getType())) {
return Pair.of(metadataEntry.getKey(),
new PipeConfValue(SECRET_METADATA_TYPE, SECRET_MASK_VALUE));
} else {
return Pair.of(metadataEntry.getKey(), metadataEntry.getValue());
}
})
.collect(Collectors.toMap(Pair::getKey, Pair::getValue));
} else {
return parsedData.entrySet().stream()
.filter(metadataEntry -> keysToRetrieve.contains(metadataEntry.getKey()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.epam.pipeline.AbstractSpringTest;
import com.epam.pipeline.controller.vo.EntityVO;
import com.epam.pipeline.controller.vo.MetadataVO;
import com.epam.pipeline.dao.metadata.MetadataDao;
import com.epam.pipeline.entity.metadata.MetadataEntry;
import com.epam.pipeline.entity.metadata.PipeConfValue;
import com.epam.pipeline.entity.preference.Preference;
Expand Down Expand Up @@ -248,10 +249,11 @@ public void testThatSecretMetadataWillBeHidedDuringSearch() {
Assert.assertFalse(loadResultByKey.isEmpty());
Assert.assertFalse(loadResultByKey.get(0).getData().isEmpty());

// But we can't see secret value when list all metadata for the entity
// But we can't see the value of the secret when list all metadata for the entity
List<MetadataEntry> loadResult = metadataManager.listMetadataItems(Collections.singletonList(entityVO));
Assert.assertFalse(loadResult.isEmpty());
Assert.assertTrue(loadResult.get(0).getData().isEmpty());
Assert.assertFalse(loadResult.get(0).getData().isEmpty());
Assert.assertEquals(MetadataDao.SECRET_MASK_VALUE, loadResult.get(0).getData().get(KEY_1).getValue());
}

@Test(expected = MetadataReadingException.class)
Expand Down