-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Resolve system properties/environment variables while browsing the
application.properties values Fixes #448 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
8b3973d
commit d0d87ac
Showing
8 changed files
with
225 additions
and
73 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
79 changes: 79 additions & 0 deletions
79
...4mp.ls/src/main/java/org/eclipse/lsp4mp/extensions/sysenv/SysEnvItemMetadataProvider.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,79 @@ | ||
package org.eclipse.lsp4mp.extensions.sysenv; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import org.eclipse.lsp4mp.commons.metadata.ItemMetadata; | ||
import org.eclipse.lsp4mp.commons.utils.StringUtils; | ||
import org.eclipse.lsp4mp.extensions.ExtendedMicroProfileProjectInfo; | ||
import org.eclipse.lsp4mp.extensions.ItemMetadataProvider; | ||
import org.eclipse.lsp4mp.model.PropertiesModel; | ||
|
||
/** | ||
* Properties provider for Environment variables and System properties. | ||
*/ | ||
public class SysEnvItemMetadataProvider implements ItemMetadataProvider { | ||
|
||
private List<ItemMetadata> sysEnvProperties; | ||
|
||
public SysEnvItemMetadataProvider(ExtendedMicroProfileProjectInfo projectInfo) { | ||
|
||
} | ||
|
||
@Override | ||
public boolean isAvailable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void update(PropertiesModel document) { | ||
this.sysEnvProperties = collectSysEnvProperties(); | ||
} | ||
|
||
@Override | ||
public List<ItemMetadata> getProperties() { | ||
return sysEnvProperties; | ||
} | ||
|
||
private static List<ItemMetadata> collectSysEnvProperties() { | ||
Stream<ItemMetadata> sysProps = System.getProperties().entrySet().stream().map(e -> { | ||
String name = e.getKey().toString(); | ||
String defaultValue = e.getValue() != null ? e.getValue().toString() : null; | ||
|
||
ItemMetadata item = new ItemMetadata(); | ||
item.setExtensionName("System property"); | ||
item.setName(name); | ||
item.setDefaultValue(defaultValue); | ||
item.setType("java.lang.String"); | ||
return item; | ||
}); | ||
|
||
Stream<ItemMetadata> envVars = System.getenv().entrySet().stream().map(e -> { | ||
String name = e.getKey(); | ||
// Poor-man obfuscation of env var keys (*_KEY) and secrets (*_SECRET) | ||
// Maybe later add configuration for suffixes and/or actual keys to obfuscate | ||
String defaultValue = obfuscate(name, e.getValue()); | ||
|
||
ItemMetadata item = new ItemMetadata(); | ||
item.setExtensionName("Environment variable"); | ||
item.setName(name); | ||
item.setDefaultValue(defaultValue); | ||
item.setType("java.lang.String"); | ||
return item; | ||
}); | ||
|
||
return Stream.concat(sysProps, envVars)// | ||
.collect(Collectors.toList()); | ||
|
||
} | ||
|
||
private static String obfuscate(String key, String value) { | ||
if (StringUtils.isEmpty(key)) { | ||
return key; | ||
} | ||
String upKey = key.toUpperCase().replace(".", "_"); | ||
return upKey.endsWith("_KEY") || upKey.endsWith("_SECRET") || upKey.endsWith("_PASSOWORD") | ||
|| upKey.endsWith("_TOKEN") ? "*********" : value; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...src/main/java/org/eclipse/lsp4mp/extensions/sysenv/SysEnvItemMetadataProviderFactory.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,33 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat Inc. and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lsp4mp.extensions.sysenv; | ||
|
||
import org.eclipse.lsp4mp.extensions.ExtendedMicroProfileProjectInfo; | ||
import org.eclipse.lsp4mp.extensions.ItemMetadataProvider; | ||
import org.eclipse.lsp4mp.extensions.ItemMetadataProviderFactory; | ||
|
||
/** | ||
* Factory for creating {@link ItemMetadataProvider} instance for System | ||
* properties and Environment variables. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class SysEnvItemMetadataProviderFactory implements ItemMetadataProviderFactory { | ||
|
||
@Override | ||
public ItemMetadataProvider create(ExtendedMicroProfileProjectInfo projectInfo) { | ||
return new SysEnvItemMetadataProvider(projectInfo); | ||
} | ||
} |
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.