subbareddyalamur commented on code in PR #1143: URL: https://github.com/apache/guacamole-client/pull/1143#discussion_r3108738897
########## extensions/guacamole-vault/modules/guacamole-vault-openbao/src/main/java/org/apache/guacamole/vault/openbao/secret/OpenBaoSecretService.java: ########## @@ -0,0 +1,172 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.guacamole.vault.openbao.secret; + +import com.google.gson.JsonObject; +import com.google.inject.Inject; +import org.apache.guacamole.GuacamoleException; +import org.apache.guacamole.net.auth.Connectable; +import org.apache.guacamole.net.auth.UserContext; +import org.apache.guacamole.protocol.GuacamoleConfiguration; +import org.apache.guacamole.token.TokenFilter; +import org.apache.guacamole.vault.secret.VaultSecretService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; + +/** + * OpenBao implementation of VaultSecretService. + * Retrieves RDP passwords from OpenBao based on the logged-in Guacamole username. + */ +public class OpenBaoSecretService implements VaultSecretService { + + /** + * Logger for this class. + */ + private static final Logger logger = LoggerFactory.getLogger(OpenBaoSecretService.class); + + /** + * Client for communicating with OpenBao. + */ + @Inject + private OpenBaoClient openBaoClient; + + /** + * Constructor that logs when the service is created. + */ + public OpenBaoSecretService() { + logger.info("OpenBaoSecretService initialized"); + } + + /** + * The token pattern for OpenBao secrets: ${OPENBAO_SECRET} + */ + public static final String OPENBAO_SECRET_TOKEN = "${OPENBAO_SECRET}"; + + /** + * The token pattern for Guacamole username: ${GUAC_USERNAME} + */ + public static final String GUAC_USERNAME_TOKEN = "${GUAC_USERNAME}"; + + @Override + public String canonicalize(String token) { + // Return the canonical form for tokens we recognize + if (token == null) + return null; + + // Remove ${} wrapper and return just the token name + if (OPENBAO_SECRET_TOKEN.equals(token)) { + return "OPENBAO_SECRET"; + } + + if (GUAC_USERNAME_TOKEN.equals(token)) { + return "GUAC_USERNAME"; + } + + // Not our token + return null; + } + + @Override + public Future<String> getValue(String token) throws GuacamoleException { + // This method is called for simple token lookups without user context + logger.warn("getValue(String) called without user context - cannot determine username"); + return CompletableFuture.completedFuture(null); + } + + @Override + public Future<String> getValue(UserContext userContext, Connectable connectable, String token) + throws GuacamoleException { + + logger.info("getValue() called with token: {}", token); + + // Get the logged-in Guacamole username + String username = userContext.self().getIdentifier(); + + // Handle GUAC_USERNAME token - return the Guacamole username + if ("GUAC_USERNAME".equals(token)) { + logger.info("getValue() returning username: '{}'", username); + return CompletableFuture.completedFuture(username); + } + + // Handle OPENBAO_SECRET token - fetch password from OpenBao + if ("OPENBAO_SECRET".equals(token)) { Review Comment: I've implemented that additively in this new commit, so arbitrary secret paths are now supported alongside the existing per-user lookup. I'd like to avoid renaming ${OPENBAO_SECRET} in this PR to "${OPENBAO_PASSWORD}" -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
