Skip to content

Commit

Permalink
Merge pull request #14 from ibuildthecloud/master
Browse files Browse the repository at this point in the history
Better errors response for failing TPM inventory calls
  • Loading branch information
ibuildthecloud authored Oct 29, 2021
2 parents bdf5642 + 0bdbf34 commit b06e6c0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/cacerts/cacerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func get(server, token, path string, clusterToken bool) ([]byte, string, error)

data, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, "", fmt.Errorf("%s: %s", resp.Status, data)
return nil, "", fmt.Errorf("%s: %s", data, resp.Status)
}
return data, caChecksum, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func processRemote(cfg Config) (Config, error) {
logrus.Infof("server and token set but required role is not set. Trying to bootstrapping config from machine inventory")
resp, _, err := cacerts.MachineGet(cfg.Server, cfg.Token, "/v1-rancheros/inventory")
if err != nil {
return cfg, fmt.Errorf("bootstrapping config from machine inventory: %w", err)
return cfg, fmt.Errorf("from machine inventory: %w", err)
}

config := map[string]interface{}{}
if err := json.Unmarshal(resp, &config); err != nil {
return cfg, fmt.Errorf("decoding inventory response: %w", err)
return cfg, fmt.Errorf("inventory response: %s: %w", resp, err)
}

currentConfig, err := convert.EncodeToMap(cfg)
Expand Down
4 changes: 2 additions & 2 deletions pkg/rancherd/rancher.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *Rancherd) Info(ctx context.Context) error {
func (r *Rancherd) Upgrade(ctx context.Context, upgradeConfig UpgradeConfig) error {
cfg, err := config.Load(r.cfg.ConfigPath)
if err != nil {
return fmt.Errorf("loading config from %s: %w", r.cfg.ConfigPath, err)
return fmt.Errorf("loading config: %w", err)
}

rancherVersion, err := versions.RancherVersion(upgradeConfig.RancherVersion)
Expand Down Expand Up @@ -145,7 +145,7 @@ func (r *Rancherd) Upgrade(ctx context.Context, upgradeConfig UpgradeConfig) err
func (r *Rancherd) execute(ctx context.Context) error {
cfg, err := config.Load(r.cfg.ConfigPath)
if err != nil {
return fmt.Errorf("loading config from %s: %w", r.cfg.ConfigPath, err)
return fmt.Errorf("loading config: %w", err)
}

if err := r.setWorking(cfg); err != nil {
Expand Down
20 changes: 16 additions & 4 deletions pkg/tpm/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/google/go-attestation/attest"
"github.com/gorilla/websocket"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand All @@ -34,6 +35,11 @@ func Get(cacerts []byte, url string, header http.Header) ([]byte, error) {
return nil, err
}

hash, err := GetPubHash()
if err != nil {
return nil, err
}

token, err := getToken(attestationData)
if err != nil {
return nil, err
Expand All @@ -44,9 +50,15 @@ func Get(cacerts []byte, url string, header http.Header) ([]byte, error) {
}
header.Add("Authorization", token)
wsURL := strings.Replace(url, "http", "ws", 1)
logrus.Infof("Dialing %s with Authorization: %s", wsURL, token)
conn, _, err := dialer.Dial(wsURL, header)
logrus.Infof("Using TPMHash %s to dial %s", hash, wsURL)
conn, resp, err := dialer.Dial(wsURL, header)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusUnauthorized {
data, err := ioutil.ReadAll(resp.Body)
if err == nil {
return nil, errors.New(string(data))
}
}
return nil, err
}
defer conn.Close()
Expand All @@ -61,7 +73,7 @@ func Get(cacerts []byte, url string, header http.Header) ([]byte, error) {
return nil, fmt.Errorf("unmarshaling Challenge: %w", err)
}

resp, err := getChallengeResponse(challenge.EC, aikBytes)
challengeResp, err := getChallengeResponse(challenge.EC, aikBytes)
if err != nil {
return nil, err
}
Expand All @@ -72,7 +84,7 @@ func Get(cacerts []byte, url string, header http.Header) ([]byte, error) {
}
defer writer.Close()

if err := json.NewEncoder(writer).Encode(resp); err != nil {
if err := json.NewEncoder(writer).Encode(challengeResp); err != nil {
return nil, fmt.Errorf("encoding ChallengeResponse: %w", err)
}

Expand Down

0 comments on commit b06e6c0

Please sign in to comment.