A Kubernetes operator that bridges Hardware Security Module (HSM) data storage with Kubernetes Secrets, providing true secret portability th
1/*
2Copyright 2025.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package api
18
19import (
20 "net/http"
21
22 "github.com/gin-gonic/gin"
23)
24
25// setupProxyRoutes sets up proxy routes for HSM operations
26func (s *Server) setupProxyRoutes() {
27 // Serve web UI static files
28 s.router.Static("/web", "./web")
29 s.router.GET("/", func(c *gin.Context) {
30 c.Redirect(http.StatusFound, "/web/")
31 })
32
33 // Create API v1 group
34 v1 := s.router.Group("/api/v1")
35 {
36 // Authentication endpoints (no auth required)
37 if s.authenticator != nil {
38 authGroup := v1.Group("/auth")
39 {
40 authGroup.POST("/token", s.authenticator.HandleTokenGeneration())
41 }
42 }
43
44 // HSM operations group - use ProxyClient methods directly as handlers
45 hsmGroup := v1.Group("/hsm")
46 {
47 // HSM device info and status
48 hsmGroup.GET("/info", s.proxyClient.GetInfo)
49 hsmGroup.GET("/status", s.proxyClient.IsConnected)
50
51 // Secret operations
52 secretsGroup := hsmGroup.Group("/secrets")
53 {
54 // List secrets
55 secretsGroup.GET("", s.proxyClient.ListSecrets)
56
57 // Secret-specific operations
58 secretsGroup.GET("/:path", s.proxyClient.ReadSecret)
59 secretsGroup.POST("/:path", s.proxyClient.WriteSecret)
60 secretsGroup.PUT("/:path", s.proxyClient.WriteSecret)
61 secretsGroup.DELETE("/:path", s.proxyClient.DeleteSecret)
62 secretsGroup.DELETE("/:path/:key", s.proxyClient.DeleteSecretKey)
63
64 // Secret metadata and checksum
65 secretsGroup.GET("/:path/metadata", s.proxyClient.ReadMetadata)
66 secretsGroup.GET("/:path/checksum", s.proxyClient.GetChecksum)
67 }
68
69 // PIN operations
70 hsmGroup.POST("/change-pin", s.proxyClient.ChangePIN)
71
72 // Mirror operations
73 hsmGroup.POST("/mirror/sync", s.handleMirrorSync)
74 }
75
76 // Health and info endpoints can stay local
77 v1.GET("/health", s.handleHealth)
78 v1.GET("/info", s.handleInfo)
79 }
80}
81
82// handleInfo provides information about the API proxy
83func (s *Server) handleInfo(c *gin.Context) {
84 info := map[string]any{
85 "service": "HSM Secrets Operator API",
86 "version": "v1alpha1",
87 "mode": "proxy",
88 "description": "Proxies HSM operations to agent pods",
89 }
90
91 s.sendResponse(c, http.StatusOK, "API information", info)
92}