Skip to content

Commit bbfec79

Browse files
committed
get single secret with value
1 parent d10609e commit bbfec79

1 file changed

Lines changed: 9 additions & 20 deletions

File tree

api/main.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func main() {
260260
// Get all secrets
261261
api.GET("/secrets", getSecrets)
262262
// Get a specific secret
263-
api.GET("/secrets/:id", getSecret)
263+
api.GET("/secrets/:name", getSecret)
264264
// Create a new secret
265265
api.POST("/secrets", createSecret)
266266
// Update a secret
@@ -326,39 +326,28 @@ func getSecrets(c *gin.Context) {
326326
}
327327

328328
func getSecret(c *gin.Context) {
329-
id := c.Param("id")
329+
name := c.Param("name") // GET /api/v1/secrets/:name
330330

331-
// Get userID from context (set by authMiddleware)
332331
userID, exists := c.Get("userID")
333332
if !exists {
334333
c.JSON(http.StatusUnauthorized, gin.H{"error": "User ID not found in context"})
335334
return
336335
}
337-
338336
userIDStr := userID.(string)
337+
339338
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
340339
defer cancel()
341340

342-
// Find secret by ID and userID to ensure ownership
343-
var secret Secret
344-
err := secretsCollection.FindOne(ctx, bson.M{"_id": id, "userId": userIDStr}).Decode(&secret)
341+
// Decrypt all fields via vault service
342+
decryptedData, err := vaultSvc.GetSecret(ctx, userIDStr, name)
345343
if err != nil {
346-
if err == mongo.ErrNoDocuments {
347-
c.JSON(http.StatusNotFound, gin.H{"error": "Secret not found"})
348-
return
349-
}
350-
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch secret: " + err.Error()})
344+
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
351345
return
352346
}
353347

354-
c.JSON(http.StatusOK, map[string]interface{}{
355-
"id": secret.ID,
356-
"userId": secret.UserID,
357-
"name": secret.Name,
358-
"description": secret.Description,
359-
"data": secret.Data,
360-
"createdAt": secret.CreatedAt.Format(time.RFC3339),
361-
"updatedAt": secret.UpdatedAt.Format(time.RFC3339),
348+
c.JSON(http.StatusOK, gin.H{
349+
"name": name,
350+
"data": decryptedData, // map[string]string{"A":"123", "DBNAME":"mydbname", ...}
362351
})
363352
}
364353

0 commit comments

Comments
 (0)