-
Notifications
You must be signed in to change notification settings - Fork 95
[KeyManager] Introduce Key Management Agent #749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NilanjanDaw
wants to merge
6
commits into
google:main
Choose a base branch
from
NilanjanDaw:add-kma-launcher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0d42734
[KeyManager] Introduce independent Key Management Agent with KEY_PROT…
NilanjanDaw 01e87f1
minor lint and bug fixes.
NilanjanDaw ffb9583
fix todo and use mode proto
NilanjanDaw 34ed09c
fix go mod versions
NilanjanDaw 0bc27bc
address review comments
NilanjanDaw 1bf6961
lint fix
NilanjanDaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| .git | ||
| .github | ||
| .gitignore | ||
| .gitmodules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Multi-stage Dockerfile for Keymanager (WSD) | ||
| FROM golang:1.24 AS go-builder | ||
|
|
||
| # Install Rust | ||
| RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y | ||
| ENV PATH="/root/.cargo/bin:${PATH}" | ||
|
|
||
| # Install dependencies for BoringSSL and others | ||
| RUN apt-get update && apt-get install -y cmake g++ libssl-dev pkg-config clang libclang-dev | ||
| RUN cargo install bindgen-cli | ||
|
|
||
| WORKDIR /app | ||
| COPY . . | ||
|
|
||
| # Build the Rust part | ||
| RUN cd keymanager && go generate ./... | ||
|
|
||
| # Build the Go binary | ||
| RUN cd keymanager && go build -o /kma ./cmd/agent/main.go | ||
|
|
||
| FROM debian:trixie-slim | ||
| RUN apt-get update && apt-get install -y libssl3 && rm -rf /var/lib/apt/lists/* | ||
| COPY --from=go-builder /kma /kma | ||
|
|
||
| ENTRYPOINT ["/kma"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| steps: | ||
| # 1. Build the Key Management Agent image | ||
| - name: 'gcr.io/cloud-builders/docker' | ||
| args: ['build', '-t', 'kma-agent', '-f', 'keymanager/cmd/agent/Dockerfile', '.'] | ||
|
|
||
| # 2. Start the container in the background | ||
| # We use a shared volume to access the UDS from the host/next step | ||
| - name: 'gcr.io/cloud-builders/docker' | ||
| id: 'start-kma' | ||
| entrypoint: 'bash' | ||
| args: | ||
| - '-c' | ||
| - | | ||
| mkdir -p /workspace/socket | ||
| docker run -d --name kma-test \ | ||
| -v /workspace/socket:/run/container_launcher \ | ||
| kma-agent | ||
|
|
||
| # Wait for the socket to be created | ||
| timeout 30s bash -c 'until [ -S /workspace/socket/kmaserver.sock ]; do sleep 1; done' | ||
|
|
||
| # 3. Test the Capabilities API | ||
| - name: 'gcr.io/cloud-builders/curl' | ||
| id: 'test-capabilities' | ||
| entrypoint: 'bash' | ||
| args: | ||
| - '-c' | ||
| - | | ||
| curl -s --unix-socket /workspace/socket/kmaserver.sock http://localhost/v1/capabilities | grep "KEM_ALGORITHM_DHKEM_X25519_HKDF_SHA256" | ||
|
|
||
| # 4. Test the Enumerate Keys API | ||
| - name: 'gcr.io/cloud-builders/curl' | ||
| id: 'test-enumerate' | ||
| entrypoint: 'bash' | ||
| args: | ||
| - '-c' | ||
| - | | ||
| curl -s --unix-socket /workspace/socket/kmaserver.sock http://localhost/v1/keys | grep "key_infos" | ||
|
|
||
| # 5. Cleanup | ||
| - name: 'gcr.io/cloud-builders/docker' | ||
| args: ['rm', '-f', 'kma-test'] | ||
| waitFor: ['test-capabilities', 'test-enumerate'] | ||
|
|
||
| options: | ||
| logging: CLOUD_LOGGING_ONLY |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // package main is the entrypoint for the keymanager workload service daemon. | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "path/filepath" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| keyprotectionservice "github.com/google/go-tpm-tools/keymanager/key_protection_service" | ||
| keymanager "github.com/google/go-tpm-tools/keymanager/km_common/proto" | ||
| workloadservice "github.com/google/go-tpm-tools/keymanager/workload_service" | ||
| ) | ||
|
|
||
| func main() { | ||
| socketPath := flag.String("socket", "/run/container_launcher/kmaserver.sock", "Path to the unix socket") | ||
| kpsPort := flag.Int("kps-port", 50050, "Port for the KPS gRPC server") | ||
| flag.Parse() | ||
|
|
||
| ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||
| defer stop() | ||
|
|
||
| mode := parseEnvEnum("KEY_PROTECTION_MECHANISM", keymanager.KeyProtectionMechanism_KEY_PROTECTION_VM_EMULATED, keymanager.KeyProtectionMechanism_value) | ||
| role := parseEnvEnum("SERVICE_ROLE", keymanager.ServiceRole_WSD, keymanager.ServiceRole_value) | ||
|
|
||
| log.Printf("Starting KeyManager launcher. Mode: %s, Role: %s\n", mode, role) | ||
|
|
||
| var err error | ||
| if mode == keymanager.KeyProtectionMechanism_KEY_PROTECTION_VM && role == keymanager.ServiceRole_KPS { | ||
| err = runKPS(ctx, *kpsPort) | ||
| } else { | ||
| err = runWSD(ctx, *socketPath, mode) | ||
| } | ||
|
|
||
| if err != nil { | ||
| log.Fatalf("Server exited with error: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func runWSD(ctx context.Context, socketPath string, mode keymanager.KeyProtectionMechanism) error { | ||
| socketDir := filepath.Dir(socketPath) | ||
| if err := os.MkdirAll(socketDir, 0755); err != nil { | ||
| return fmt.Errorf("failed to create directory for socket %s: %w", socketDir, err) | ||
| } | ||
|
|
||
| log.Printf("Initializing KeyManager WSD server on unix socket %s", socketPath) | ||
| srv, err := workloadservice.New(ctx, socketPath, mode) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create WSD server: %w", err) | ||
| } | ||
|
|
||
| errChan := make(chan error, 1) | ||
| go func() { | ||
| if err := srv.Serve(); err != nil && err != http.ErrServerClosed { | ||
| errChan <- fmt.Errorf("unix socket server failed: %w", err) | ||
| } | ||
| }() | ||
|
|
||
| select { | ||
| case err := <-errChan: | ||
| return err | ||
| case <-ctx.Done(): | ||
| log.Println("Shutting down WSD server...") | ||
| shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
| defer cancel() | ||
| if err := srv.Shutdown(shutdownCtx); err != nil { | ||
| return fmt.Errorf("error during unix socket shutdown: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func runKPS(ctx context.Context, port int) error { | ||
| log.Printf("Initializing Key Protection Service on TCP port %d", port) | ||
| srv, err := keyprotectionservice.NewServer(port) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create KPS server: %w", err) | ||
| } | ||
|
|
||
| errChan := make(chan error, 1) | ||
| go func() { | ||
| if err := srv.Serve(); err != nil { | ||
| errChan <- fmt.Errorf("gRPC server failed: %w", err) | ||
| } | ||
| }() | ||
|
|
||
| select { | ||
| case err := <-errChan: | ||
| return err | ||
| case <-ctx.Done(): | ||
| log.Println("Shutting down KPS server...") | ||
| shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
| defer cancel() | ||
| if err := srv.Shutdown(shutdownCtx); err != nil { | ||
| return fmt.Errorf("error during gRPC shutdown: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func parseEnvEnum[T ~int32](key string, defaultValue T, enumMap map[string]int32) T { | ||
| val := os.Getenv(key) | ||
| if val == "" { | ||
| return defaultValue | ||
| } | ||
| v, ok := enumMap[val] | ||
| if !ok { | ||
| log.Fatalf("Unrecognized %s: %s", key, val) | ||
| } | ||
| return T(v) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add .dockerignore to avoid copying all the files like
.git?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack added a .gitignore file