Skip to content

Commit

Permalink
Build Time Improvement (#1067) (#1069)
Browse files Browse the repository at this point in the history
* 🚧 build(VSecM): parallelize building images



* minor

* minor

* move vendor

* removed proto generation from bundle.sh

* even faster

* typo

* code cleanup

---------

Signed-off-by: Volkan Özçelik <[email protected]>
  • Loading branch information
v0lkan authored Jul 13, 2024
1 parent f96536d commit 6f29299
Show file tree
Hide file tree
Showing 26 changed files with 215 additions and 193 deletions.
2 changes: 2 additions & 0 deletions app/safe/internal/server/route/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func Delete(

// Only sentinel can execute delete requests.
if ok, respond := validation.IsSentinel(j, cid, spiffeid); !ok {
j.Event = audit.NotSentinel
journal.Log(j)
respond(w)
return
}
Expand Down
6 changes: 2 additions & 4 deletions app/safe/internal/server/route/keystone/keystone.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,9 @@ func Status(

// Only sentinel can get the status.
if ok, respond := validation.IsSentinel(j, cid, spiffeid); !ok {
respond(w)

j.Event = audit.BadSpiffeId
j.Event = audit.NotSentinel
journal.Log(j)

respond(w)
return
}

Expand Down
2 changes: 2 additions & 0 deletions app/safe/internal/server/route/list/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func doList(

// Only sentinel can list.
if ok, respond := validation.IsSentinel(j, cid, spiffeid); !ok {
j.Event = audit.NotSentinel
journal.Log(j)
respond(w)
return
}
Expand Down
12 changes: 7 additions & 5 deletions app/safe/internal/server/route/receive/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ func Keys(cid string, r *http.Request, w http.ResponseWriter) {
// Only sentinel can set keys.
if ok, respond := validation.IsSentinel(j, cid, spiffeid); !ok {
respond(w)

j.Event = audit.BadSpiffeId
j.Event = audit.NotSentinel
journal.Log(j)

return
}

Expand Down Expand Up @@ -105,8 +103,12 @@ func Keys(cid string, r *http.Request, w http.ResponseWriter) {
return
}

keysCombined := agePrivateKey + "\n" + agePublicKey + "\n" + aesCipherKey
crypto.SetRootKeyInMemory(keysCombined)
rkt := data.RootKeyCollection{
PrivateKey: agePrivateKey,
PublicKey: agePublicKey,
AesSeed: aesCipherKey,
}
crypto.SetRootKeyInMemory(rkt.Combine())

if err := bootstrap.PersistRootKeysToRootKeyBackingStore(
data.RootKeyCollection{
Expand Down
8 changes: 5 additions & 3 deletions app/safe/internal/server/route/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/vmware-tanzu/secrets-manager/app/safe/internal/server/route/base/validation"
"github.com/vmware-tanzu/secrets-manager/core/audit/journal"
"github.com/vmware-tanzu/secrets-manager/core/constants/audit"
"github.com/vmware-tanzu/secrets-manager/core/constants/val"
"github.com/vmware-tanzu/secrets-manager/core/crypto"
entity "github.com/vmware-tanzu/secrets-manager/core/entity/v1/data"
log "github.com/vmware-tanzu/secrets-manager/core/log/std"
Expand All @@ -43,7 +44,7 @@ func Secret(cid string, r *http.Request, w http.ResponseWriter) {
spiffeid := s.IdAsString(r)
if spiffeid == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := io.WriteString(w, "NOK!")
_, err := io.WriteString(w, val.NotOk)
if err != nil {
log.ErrorLn(&cid, "error writing response", err.Error())
}
Expand All @@ -53,7 +54,7 @@ func Secret(cid string, r *http.Request, w http.ResponseWriter) {

if !crypto.RootKeySetInMemory() {
w.WriteHeader(http.StatusBadRequest)
_, err := io.WriteString(w, "NOK!")
_, err := io.WriteString(w, val.NotOk)
if err != nil {
log.ErrorLn(&cid, "error writing response", err.Error())
}
Expand All @@ -67,8 +68,9 @@ func Secret(cid string, r *http.Request, w http.ResponseWriter) {

// Only sentinel can do this.
if ok, respond := validation.IsSentinel(j, cid, spiffeid); !ok {
j.Event = audit.NotSentinel
journal.Log(j)
respond(w)

return
}

Expand Down
56 changes: 56 additions & 0 deletions app/safe/internal/state/secret/collection/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package collection

import (
"errors"
"os"
"strings"

"github.com/vmware-tanzu/secrets-manager/app/safe/internal/state/io"
"github.com/vmware-tanzu/secrets-manager/app/safe/internal/state/stats"
f "github.com/vmware-tanzu/secrets-manager/core/constants/file"
"github.com/vmware-tanzu/secrets-manager/core/env"
log "github.com/vmware-tanzu/secrets-manager/core/log/std"
)

func populateSecretsFromFileStore(cid string) error {
root := env.DataPathForSafe()
files, err := os.ReadDir(root)
if err != nil {
return errors.Join(
err,
errors.New("populateSecrets: problem reading secrets directory"),
)
}

for _, file := range files {
if file.IsDir() {
continue
}

fn := file.Name()
if strings.HasSuffix(fn, f.AgeBackupExtension) {
continue
}

key := strings.Replace(fn, f.AgeExtension, "", 1)

_, exists := Secrets.Load(key)
if exists {
continue
}

secretOnDisk, err := io.ReadFromDisk(key)
if err != nil {
log.ErrorLn(&cid,
"populateSecrets: problem reading secret from disk:",
err.Error())
continue
}
if secretOnDisk != nil {
stats.CurrentState.Increment(key, Secrets.Load)
Secrets.Store(key, *secretOnDisk)
}
}

return nil
}
49 changes: 0 additions & 49 deletions app/safe/internal/state/secret/collection/populate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@
package collection

import (
"errors"
"os"
"strings"
"sync"

"github.com/vmware-tanzu/secrets-manager/app/safe/internal/state/io"
"github.com/vmware-tanzu/secrets-manager/app/safe/internal/state/stats"
f "github.com/vmware-tanzu/secrets-manager/core/constants/file"
"github.com/vmware-tanzu/secrets-manager/core/entity/v1/data"
"github.com/vmware-tanzu/secrets-manager/core/env"
log "github.com/vmware-tanzu/secrets-manager/core/log/std"
Expand All @@ -39,49 +33,6 @@ func SecretsPopulated() bool {
return secretsPopulated
}

func populateSecretsFromFileStore(cid string) error {
root := env.DataPathForSafe()
files, err := os.ReadDir(root)
if err != nil {
return errors.Join(
err,
errors.New("populateSecrets: problem reading secrets directory"),
)
}

for _, file := range files {
if file.IsDir() {
continue
}

fn := file.Name()
if strings.HasSuffix(fn, f.AgeBackupExtension) {
continue
}

key := strings.Replace(fn, f.AgeExtension, "", 1)

_, exists := Secrets.Load(key)
if exists {
continue
}

secretOnDisk, err := io.ReadFromDisk(key)
if err != nil {
log.ErrorLn(&cid,
"populateSecrets: problem reading secret from disk:",
err.Error())
continue
}
if secretOnDisk != nil {
stats.CurrentState.Increment(key, Secrets.Load)
Secrets.Store(key, *secretOnDisk)
}
}

return nil
}

// PopulateSecrets scans the designated secrets storage directory on disk,
// reading each secret file that is not marked as a backup, and loads the
// secrets into a global store if they have not already been loaded. This
Expand Down
19 changes: 2 additions & 17 deletions app/sentinel/internal/oidc/engine/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,9 @@ import (
"github.com/vmware-tanzu/secrets-manager/core/constants/key"
"github.com/vmware-tanzu/secrets-manager/core/crypto"
entity "github.com/vmware-tanzu/secrets-manager/core/entity/v1/data"
"github.com/vmware-tanzu/secrets-manager/core/entity/v1/reqres/sentinel"
)

// SecretRequest encapsulates a VSecM Safe REST command payload.
type SecretRequest struct {
Workloads []string `json:"workload"`
Secret string `json:"secret"`
Namespaces []string `json:"namespaces,omitempty"`
Encrypt bool `json:"encrypt,omitempty"`
Delete bool `json:"delete,omitempty"`
Append bool `json:"append,omitempty"`
List bool `json:"list,omitempty"`
Template string `json:"template,omitempty"`
Format string `json:"format,omitempty"`
SerializedRootKeys string `json:"root-keys,omitempty"`
NotBefore string `json:"nbf,omitempty"`
Expires string `json:"exp,omitempty"`
}

// HandleCommandSecrets processes HTTP requests related to secret management.
//
// This function handles both listing and modifying secrets based on the
Expand Down Expand Up @@ -81,7 +66,7 @@ type SecretRequest struct {
// - If there is an error during secret retrieval or modification, it returns
// a 500 Internal Server Error status with the error message.
func HandleCommandSecrets(
w http.ResponseWriter, r *http.Request, req *SecretRequest,
w http.ResponseWriter, r *http.Request, req *sentinel.SecretRequest,
) {
id := crypto.Id()

Expand Down
4 changes: 3 additions & 1 deletion app/sentinel/internal/oidc/engine/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ package engine
import (
"encoding/json"
"net/http"

"github.com/vmware-tanzu/secrets-manager/core/entity/v1/reqres/sentinel"
)

// HandleSecrets processes incoming HTTP requests related to secrets management.
Expand All @@ -39,7 +41,7 @@ func HandleSecrets(w http.ResponseWriter, r *http.Request) {
return
}

var req SecretRequest
var req sentinel.SecretRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down
3 changes: 2 additions & 1 deletion app/sentinel/internal/oidc/safe/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ func Post(
},
}

parts := strings.Split(sc.SerializedRootKeys, "\n")
parts := sc.SplitRootKeys()

if len(parts) != 3 {
return "", printPayloadError(
cid, errors.New("post: Bad data! Very bad data"))
Expand Down
2 changes: 1 addition & 1 deletion app/sentinel/internal/safe/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func Post(parentContext context.Context,
},
}

parts := strings.Split(sc.SerializedRootKeys, "\n")
parts := sc.SplitRootKeys()
if len(parts) != 3 {
return errors.New("post: Bad data! Very bad data")
}
Expand Down
2 changes: 1 addition & 1 deletion core/audit/journal/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
package journal

import (
"github.com/vmware-tanzu/secrets-manager/core/constants/audit"
"net/http"

"github.com/vmware-tanzu/secrets-manager/core/constants/audit"
"github.com/vmware-tanzu/secrets-manager/core/entity/v1/data"
)

Expand Down
18 changes: 10 additions & 8 deletions core/constants/audit/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ package audit

type Event string

const Enter Event = "vsecm-enter"
const BadPayload Event = "vsecm-bad-payload"
const BadPeerSvid Event = "vsecm-bad-peer-spiffeid"
const BadSpiffeId Event = "vsecm-bad-spiffeid"
const BrokenBody Event = "vsecm-broken-body"
const RequestTypeMismatch Event = "vsecm-request-type-mismatch"
const BadPeerSvid Event = "vsecm-bad-peer-spiffeid"
const DecryptionFailed Event = "vsecm-decryption-failed"
const EncryptionFailed Event = "vsecm-encryption-failed"
const Enter Event = "vsecm-enter"
const NoSecret Event = "vsecm-no-secret"
const Ok Event = "vsecm-ok"
const NoWorkloadId Event = "vsecm-no-wl-id"
const NoValue Event = "vsecm-no-value"
const EncryptionFailed Event = "vsecm-encryption-failed"
const DecryptionFailed Event = "vsecm-decryption-failed"
const BadPayload Event = "vsecm-bad-payload"
const NotWorkload = "vsecm-not-workload"
const NoWorkloadId Event = "vsecm-no-wl-id"
const NotSentinel = "vsecm-not-sentinel"
const Ok Event = "vsecm-ok"
const RequestTypeMismatch Event = "vsecm-request-type-mismatch"
const RootKeyNotSet = "vsecm-root-key-not-set"
1 change: 1 addition & 0 deletions core/constants/val/val.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const TimeNever = "never"
const BlankRootKey = "{}"

const Ok = "OK"
const NotOk = "NOK!"

// JsonEmpty is a constant string representing an empty value.
// This value is generated by the go templating engine
Expand Down
17 changes: 17 additions & 0 deletions core/entity/v1/data/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

package data

import "strings"

// VSecMInternalCommand is the command that VSecM uses to perform
// internal operations.
type VSecMInternalCommand struct {
Expand All @@ -33,3 +35,18 @@ type SentinelCommand struct {
ShouldSleep bool
SleepIntervalMs int
}

// SplitRootKeys splits the SerializedRootKeys of the SentinelCommand
// into a slice of strings based on newline characters.
//
// It returns a slice of strings, where each string represents a root key.
// If there are no newline characters in SerializedRootKeys, the returned
// slice will contain a single element.
//
// Example:
//
// sc := SentinelCommand{SerializedRootKeys: "key1\nkey2\nkey3"}
// keys := sc.SplitRootKeys() // returns []string{"key1", "key2", "key3"}
func (sc SentinelCommand) SplitRootKeys() []string {
return strings.Split(sc.SerializedRootKeys, "\n")
}
27 changes: 27 additions & 0 deletions core/entity/v1/reqres/sentinel/sentinel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
| Protect your secrets, protect your sensitive data.
: Explore VMware Secrets Manager docs at https://vsecm.com/
</
<>/ keep your secrets… secret
>/
<>/' Copyright 2023–present VMware Secrets Manager contributors.
>/' SPDX-License-Identifier: BSD-2-Clause
*/

package sentinel

// SecretRequest encapsulates a VSecM Safe REST command payload.
type SecretRequest struct {
Workloads []string `json:"workload"`
Secret string `json:"secret"`
Namespaces []string `json:"namespaces,omitempty"`
Encrypt bool `json:"encrypt,omitempty"`
Delete bool `json:"delete,omitempty"`
Append bool `json:"append,omitempty"`
List bool `json:"list,omitempty"`
Template string `json:"template,omitempty"`
Format string `json:"format,omitempty"`
SerializedRootKeys string `json:"root-keys,omitempty"`
NotBefore string `json:"nbf,omitempty"`
Expires string `json:"exp,omitempty"`
}
Loading

0 comments on commit 6f29299

Please sign in to comment.