Skip to content

Commit 0bdbf34

Browse files
Better errors response for failing TPM inventory calls
1 parent 357fd68 commit 0bdbf34

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

pkg/cacerts/cacerts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func get(server, token, path string, clusterToken bool) ([]byte, string, error)
9999

100100
data, err := ioutil.ReadAll(resp.Body)
101101
if resp.StatusCode != http.StatusOK {
102-
return nil, "", fmt.Errorf("%s: %s", resp.Status, data)
102+
return nil, "", fmt.Errorf("%s: %s", data, resp.Status)
103103
}
104104
return data, caChecksum, err
105105
}

pkg/config/remote.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ func processRemote(cfg Config) (Config, error) {
1818
logrus.Infof("server and token set but required role is not set. Trying to bootstrapping config from machine inventory")
1919
resp, _, err := cacerts.MachineGet(cfg.Server, cfg.Token, "/v1-rancheros/inventory")
2020
if err != nil {
21-
return cfg, fmt.Errorf("bootstrapping config from machine inventory: %w", err)
21+
return cfg, fmt.Errorf("from machine inventory: %w", err)
2222
}
2323

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

2929
currentConfig, err := convert.EncodeToMap(cfg)

pkg/rancherd/rancher.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (r *Rancherd) Info(ctx context.Context) error {
6060
func (r *Rancherd) Upgrade(ctx context.Context, upgradeConfig UpgradeConfig) error {
6161
cfg, err := config.Load(r.cfg.ConfigPath)
6262
if err != nil {
63-
return fmt.Errorf("loading config from %s: %w", r.cfg.ConfigPath, err)
63+
return fmt.Errorf("loading config: %w", err)
6464
}
6565

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

151151
if err := r.setWorking(cfg); err != nil {

pkg/tpm/get.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/google/go-attestation/attest"
1414
"github.com/gorilla/websocket"
15+
"github.com/pkg/errors"
1516
"github.com/sirupsen/logrus"
1617
)
1718

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

38+
hash, err := GetPubHash()
39+
if err != nil {
40+
return nil, err
41+
}
42+
3743
token, err := getToken(attestationData)
3844
if err != nil {
3945
return nil, err
@@ -44,9 +50,15 @@ func Get(cacerts []byte, url string, header http.Header) ([]byte, error) {
4450
}
4551
header.Add("Authorization", token)
4652
wsURL := strings.Replace(url, "http", "ws", 1)
47-
logrus.Infof("Dialing %s with Authorization: %s", wsURL, token)
48-
conn, _, err := dialer.Dial(wsURL, header)
53+
logrus.Infof("Using TPMHash %s to dial %s", hash, wsURL)
54+
conn, resp, err := dialer.Dial(wsURL, header)
4955
if err != nil {
56+
if resp != nil && resp.StatusCode == http.StatusUnauthorized {
57+
data, err := ioutil.ReadAll(resp.Body)
58+
if err == nil {
59+
return nil, errors.New(string(data))
60+
}
61+
}
5062
return nil, err
5163
}
5264
defer conn.Close()
@@ -61,7 +73,7 @@ func Get(cacerts []byte, url string, header http.Header) ([]byte, error) {
6173
return nil, fmt.Errorf("unmarshaling Challenge: %w", err)
6274
}
6375

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

75-
if err := json.NewEncoder(writer).Encode(resp); err != nil {
87+
if err := json.NewEncoder(writer).Encode(challengeResp); err != nil {
7688
return nil, fmt.Errorf("encoding ChallengeResponse: %w", err)
7789
}
7890

0 commit comments

Comments
 (0)