adb014 commented on code in PR #1143:
URL: https://github.com/apache/guacamole-client/pull/1143#discussion_r3092583947


##########
extensions/guacamole-vault/modules/guacamole-vault-openbao/src/main/java/org/apache/guacamole/vault/openbao/secret/OpenBaoClient.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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.Gson;
+import com.google.gson.JsonObject;
+import com.google.inject.Inject;
+import org.apache.guacamole.GuacamoleException;
+import org.apache.guacamole.GuacamoleServerException;
+import org.apache.guacamole.vault.openbao.conf.OpenBaoConfigurationService;
+import org.apache.hc.client5.http.classic.methods.HttpGet;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.util.Timeout;

Review Comment:
   Something like 
   
   ```java
   package org.apache.guacamole.vault.openbao.secret;
   
   import javax.inject.Inject;
   import javax.inject.Singleton;
   import java.net.URI;
   import java.time.Duration;
   import java.util.Map;
   import org.apache.guacamole.GuacamoleException;
   import org.apache.guacamole.GuacamoleServerException;
   import org.apache.guacamole.vault.openbao.conf.OpenBaoConfigurationService;
   import org.springframework.vault.VaultException;
   import org.springframework.vault.authentication.TokenAuthentication;
   import org.springframework.vault.client.VaultEndpoint;
   import org.springframework.vault.core.VaultKeyValueOperations;
   import org.springframework.vault.core.VaultTemplate;
   import org.springframework.vault.support.VaultResponse;
   import org.slf4j.Logger;
   import org.slf4j.LoggerFactory;
   
   
   public class OpenBaoClient {
       /**
        * Logger for this class.
        */
       private static final Logger logger = 
LoggerFactory.getLogger(OpenBaoClient.class);
   
       /**
        * Service for retrieving OpenBao configuration.
        */
       @Inject
       private OpenBaoConfigurationService configService;
       
       /**
        * Vault Key/Value object to retrieve values based on their key
        */
       private final VaultKeyValueOperations kvOperations;
   
   
       /**
        * KV.1 ou KV.2 template of teh mount mount
        */
       private final VaultTemplate vaultTemplate;
   
       /**
        * Complete the instantiantion of the class after injection of 
confService
        */
       @Inject
       public init() {
   
           VaultEndpoint endpoint = 
VaultEndpoint.from(configService.getServerUrl())
           );
   
           endpoint.setConnectionTimeout(
                   Duration.ofMillis(configService.getConnectionTimeout()));
           endpoint.setReadTimeout(
                   Duration.ofMillis(configService.getRequestTimeout()));
   
           this.vaultTemplate = new VaultTemplate(
                   endpoint,
                   new TokenAuthentication(configService.getToken())
           );
   
           String mountPath = configService.getMountPath();
           String kvVersion = configService.getKvVersion();
   
           if ("2".equals(kvVersion)) {
               this.kvOperations = vaultTemplate.opsForKeyValue(
                           mountPath,
                           VaultKeyValueOperations.KeyValueBackend.KV_2);
           }
           else { 
               this.kvOperations = vaultTemplate.opsForKeyValue(
                           mountPath,
                           VaultKeyValueOperations.KeyValueBackend.KV_1);
           }
       }
   
       /**
        * Retrieves a secret from OpenBao by username.
        *
        * @param username
        *     The Guacamole username to look up in OpenBao.
        *
        * @return
        *     The JSON response from OpenBao in the form of a Map.
        *
        * @throws GuacamoleException
        *     If the secret cannot be retrieved from OpenBao.
        */
       public Map<String, Object> getSecret(String username)
               throws GuacamoleException {
   
           try {
               logger.info("Fetching secret from OpenBao: key={}", username);
   
               VaultResponse response = kvOperations.get(username);
   
               if (response == null || response.getData() == null) {
                   throw new GuacamoleServerException(
                           "Secret not found in OpenBao for username: " + 
username);
               }
   
               return response.getData();
   
           } catch (VaultException e) {
               logger.error("Failed to retrieve secret from OpenBao", e);
               throw new GuacamoleServerException(
                       "Failed to retrieve secret from OpenBao", e);
           }
       }
   
       /**
        * Extracts the password field from an OpenBao KV v2 response.
        *
        * @param response
        *     The JSON response from OpenBao in the form of a 
Map<String,Object>.
        *
        * @return
        *     The password string, or null if not found.
        */
       public String extractPassword(Map<String, Object> sresponse) {
   
           if (response == null) {
               return null;
           }
   
           Object password = response.get("password");
   
           if (password instanceof String) {
               return (String) password;
           }
   
           logger.warn("Password field not found in OpenBao secret");
           return null;
       }
   }
   ```
   
   but completely untested  could reimplement your getSecret and 
extractPassword methods using a Map rather than a JsonObject 



-- 
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]

Reply via email to