Skip to content
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

Allow to provide an external base64 encoded signing key #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pixiecore/booters.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package pixiecore

import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -126,16 +127,20 @@ func (s *staticBooter) WriteBootFile(ID, io.Reader) error {
// APIBooter gets a BootSpec from a remote server over HTTP.
//
// The API is described in README.api.md
func APIBooter(url string, timeout time.Duration) (Booter, error) {
func APIBooter(url string, timeout time.Duration, signingKey [32]byte) (Booter, error) {
if !strings.HasSuffix(url, "/") {
url += "/"
}
ret := &apibooter{
client: &http.Client{Timeout: timeout},
urlPrefix: url + "v1",
key: signingKey,
}
if _, err := io.ReadFull(rand.Reader, ret.key[:]); err != nil {
return nil, fmt.Errorf("failed to get randomness for signing key: %s", err)
emptyByteVar := make([]byte, 32)
if bytes.Equal(signingKey[:], emptyByteVar) {
if _, err := io.ReadFull(rand.Reader, ret.key[:]); err != nil {
return nil, fmt.Errorf("failed to get randomness for signing key: %s", err)
}
}

return ret, nil
Expand Down
25 changes: 24 additions & 1 deletion pixiecore/cli/apicmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cli

import (
"encoding/base64"
"fmt"
"time"

Expand Down Expand Up @@ -42,10 +43,31 @@ the Pixiecore boot API. The specification can be found at <TODO>.`,
fatalf("Error reading flag: %s", err)
}

booter, err := pixiecore.APIBooter(server, timeout)
var key [32]byte

signingKey, err := cmd.Flags().GetString("signing-key")
if err != nil {
fatalf("Error reading flag: %s", err)
} else {
if signingKey != "" {
keydata, err := base64.StdEncoding.DecodeString(signingKey)
if err != nil {
fmt.Println("Error decoding signing key (it should be base64 encoded):", err)
} else {
if len(keydata) > 32 {
fatalf("Too long signin key")
} else {
copy(key[:], keydata)
}
}
}
}

booter, err := pixiecore.APIBooter(server, timeout, key)
if err != nil {
fatalf("Failed to create API booter: %s", err)
}

s := serverFromFlags(cmd)
s.Booter = booter

Expand All @@ -56,5 +78,6 @@ func init() {
rootCmd.AddCommand(apiCmd)
serverConfigFlags(apiCmd)
apiCmd.Flags().Duration("api-request-timeout", 5*time.Second, "Timeout for request to the API server")
apiCmd.Flags().String("signing-key", "", "Signing key")
// TODO: SSL cert flags for both client and server auth.
}
4 changes: 3 additions & 1 deletion pixiecore/cli/v1compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ func v1compatCLI() bool {
fatalf("cannot provide -cmdline with -api")
}

var key [32]byte

log.Printf("Starting Pixiecore in API mode, with server %s", *apiServer)
booter, err := pixiecore.APIBooter(*apiServer, *apiTimeout)
booter, err := pixiecore.APIBooter(*apiServer, *apiTimeout, key)
if err != nil {
fatalf("Failed to create API booter: %s", err)
}
Expand Down