in server/cmd/harp-server/internal/dispatchers/http/routes/backend.go [34:80]
func backend(namespace string, engine storage.Engine) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
id = r.URL.Path
keyRaw = r.URL.Query().Get("key")
)
// Remove namespace prefix
identifier := strings.TrimPrefix(id, fmt.Sprintf("/%s", namespace))
// Retrieve secret from engine
secret, err := engine.Get(ctx, identifier)
if errors.Is(err, storage.ErrSecretNotFound) {
http.Error(w, "secret not found", http.StatusNotFound)
return
}
if err != nil {
log.For(ctx).Error("unable to retrieve secret from engine", zap.Error(err), zap.String("url", r.URL.String()))
http.Error(w, "unable to retrieve secret", http.StatusBadRequest)
return
}
// key is defined
if keyRaw != "" {
// Retrieve transformer from key
transformer, err := encryption.FromKey(keyRaw)
if err != nil {
log.For(ctx).Error("unable to initialize secret transformer", zap.String("url", r.URL.String()))
http.Error(w, "unable to initialize secret transformer", http.StatusInternalServerError)
return
}
// Apply transformation to secret value
secret, err = transformer.To(ctx, secret)
if err != nil {
log.For(ctx).Error("unable to protect secret", zap.String("url", r.URL.String()))
http.Error(w, "unable to protect secret", http.StatusBadRequest)
return
}
}
// Send result
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", secret)
}
}