Skip to content

Commit

Permalink
feat: Resolve system properties/environment variables while browsing the
Browse files Browse the repository at this point in the history
application.properties values

Fixes eclipse#448

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Aug 15, 2024
1 parent 8b3973d commit 28f3d75
Show file tree
Hide file tree
Showing 14 changed files with 414 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class ItemBase {

private Boolean source;

private String origin;

public String getName() {
return name;
}
Expand Down Expand Up @@ -66,12 +68,42 @@ public void setSource(Boolean source) {
this.source = source;
}

/**
* Returns the origin of the item (Java, System / Environment variables).
*
* @return the origin of the item (Java, System / Environment variables).
*/
public String getOrigin() {
return origin;
}

/**
* Set the origin of the item (Java, System / Environment variables).
*
* @param origin the origin of the item (Java, System / Environment variables).
*/
public void setOrigin(String origin) {
this.origin = origin;
}

/**
* Returns true if the item is defined in a Java class and false otherwise (ex :
* System / Environments variable).
*
* @return true if the item is defined in a Java class and false otherwise (ex :
* System / Environments variable).
*/
public boolean isJavaOrigin() {
return origin == null;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((origin == null) ? 0 : origin.hashCode());
result = prime * result + ((source == null) ? 0 : source.hashCode());
result = prime * result + ((sourceType == null) ? 0 : sourceType.hashCode());
return result;
Expand All @@ -96,6 +128,11 @@ public boolean equals(Object obj) {
return false;
} else if (!name.equals(other.name))
return false;
if (origin == null) {
if (other.origin != null)
return false;
} else if (!origin.equals(other.origin))
return false;
if (source == null) {
if (other.source != null)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class ItemBase {

private Boolean source;

private String origin;

public String getName() {
return name;
}
Expand Down Expand Up @@ -66,12 +68,42 @@ public void setSource(Boolean source) {
this.source = source;
}

/**
* Returns the origin of the item (Java, System / Environment variables).
*
* @return the origin of the item (Java, System / Environment variables).
*/
public String getOrigin() {
return origin;
}

/**
* Set the origin of the item (Java, System / Environment variables).
*
* @param origin the origin of the item (Java, System / Environment variables).
*/
public void setOrigin(String origin) {
this.origin = origin;
}

/**
* Returns true if the item is defined in a Java class and false otherwise (ex :
* System / Environments variable).
*
* @return true if the item is defined in a Java class and false otherwise (ex :
* System / Environments variable).
*/
public boolean isJavaOrigin() {
return origin == null;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((origin == null) ? 0 : origin.hashCode());
result = prime * result + ((source == null) ? 0 : source.hashCode());
result = prime * result + ((sourceType == null) ? 0 : sourceType.hashCode());
return result;
Expand All @@ -96,6 +128,11 @@ public boolean equals(Object obj) {
return false;
} else if (!name.equals(other.name))
return false;
if (origin == null) {
if (other.origin != null)
return false;
} else if (!origin.equals(other.origin))
return false;
if (source == null) {
if (other.source != null)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public synchronized void updateSourcesProperties(List<ItemMetadata> propertiesFr

// expand properties by using new dynamic properties
expandProperties(staticProperties, dynamicProperties, getHint);
// expand properties by using old dynamic properties (coming from binary properties)
// expand properties by using old dynamic properties (coming from binary
// properties)
expandProperties(staticProperties, getDynamicProperties(), getHint);
updateListFromPropertiesSources(getProperties(), staticProperties);
updateListFromPropertiesSources(getDynamicProperties(), dynamicProperties);
Expand Down Expand Up @@ -202,7 +203,10 @@ private synchronized void synchUpdateCustomProperties(PropertiesModel document)
// - a Java sources changes (document = null)
// - a microprofile-config.properties changes (document != null)
if (document != null || provider.isAvailable()) {
List<ItemMetadata> oldProperties = new ArrayList<>(provider.getProperties());
List<ItemMetadata> oldProperties = provider.getProperties();
if (oldProperties != null) {
oldProperties = new ArrayList<>(oldProperties);
}
provider.update(document);
List<ItemMetadata> newProperties = provider.getProperties();
if (!Objects.deepEquals(oldProperties, newProperties)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************************
* 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 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");
item.setOrigin("System Properties");
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");
item.setOrigin("Environment variables");
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("_PASSWORD")
|| upKey.endsWith("_TOKEN") ? "*********" : value;
}
}
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);
}
}
Loading

0 comments on commit 28f3d75

Please sign in to comment.