From a22b996b8a32bca298d636930f1d58e8c7a7a1c2 Mon Sep 17 00:00:00 2001 From: Fred Bricon Date: Wed, 15 May 2024 11:05:24 +0200 Subject: [PATCH] feat: provide support for System properties and Environment variables Signed-off-by: Fred Bricon --- .../psi/core/SysEnvPropertiesProvider.java | 84 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 3 + 2 files changed, 87 insertions(+) create mode 100644 src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/SysEnvPropertiesProvider.java diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/SysEnvPropertiesProvider.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/SysEnvPropertiesProvider.java new file mode 100644 index 000000000..2b4e133ae --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/SysEnvPropertiesProvider.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat Inc. and others. + * All rights reserved. This program and the accompanying materials + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package com.redhat.devtools.intellij.lsp4mp4ij.psi.core; + +import com.intellij.psi.PsiModifierListOwner; +import com.intellij.util.Query; +import org.eclipse.lsp4mp.commons.metadata.ConfigurationMetadata; +import org.eclipse.lsp4mp.commons.metadata.ItemMetadata; + +import java.util.List; + +import static org.eclipse.lsp4mp.commons.metadata.ItemMetadata.CONFIG_PHASE_RUN_TIME; + +/** + * Properties provider for Environment variables and System properties. + */ +public class SysEnvPropertiesProvider extends AbstractPropertiesProvider { + @Override + protected String[] getPatterns() { + return null; + } + + @Override + protected Query createSearchPattern(SearchContext context, String pattern) { + return null; + } + + @Override + public void collectProperties(PsiModifierListOwner match, SearchContext context) { + List sysProps = System.getProperties().entrySet().stream().map(e -> { + var tuple = new String[2]; + tuple[0] = e.getKey().toString(); + tuple[1] = e.getValue().toString(); + return tuple; + }).toList(); + var sysPropsMetadata = getMetadata("System property", sysProps); + context.getCollector().merge(sysPropsMetadata, IPropertiesCollector.MergingStrategy.IGNORE_IF_EXISTS); + + + List envVars = System.getenv().entrySet().stream().map(e -> { + var tuple = new String[2]; + String key = e.getKey(); + tuple[0] = key; + // Poor-man obfuscation of env var keys (*_KEY) and secrets (*_SECRET) + // Maybe later add configuration for suffixes and/or actual keys to obfuscate + tuple[1] = obfuscate(key, e.getValue()); + return tuple; + }).toList(); + + var envVarsMetadata = getMetadata("Environment variable", envVars); + context.getCollector().merge(envVarsMetadata, IPropertiesCollector.MergingStrategy.IGNORE_IF_EXISTS); + + } + + private String obfuscate(String key, String value) { + var upKey = key.toUpperCase().replace(".", "_"); + return upKey.endsWith("_KEY") || upKey.endsWith("_SECRET") ? "*********" : value; + } + + private ConfigurationMetadata getMetadata(String source, List properties) { + ConfigurationMetadata metadata = new ConfigurationMetadata(); + List itemProperties = properties.stream().map(kv -> { + var item = new ItemMetadata(); + item.setExtensionName(source); + item.setPhase(CONFIG_PHASE_RUN_TIME); + item.setName(kv[0]); + item.setDefaultValue(kv[1]); + item.setType("java.lang.String"); + item.setDescription(source); + return item; + }).toList(); + + metadata.setProperties(itemProperties); + return metadata; + } + +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 33d86a1b2..7bde956c0 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -589,6 +589,9 @@ + +