Skip to content

Commit a70f74b

Browse files
committed
rofl/show: PrettyPrint SEK/REK in JSON Marshal
1 parent 673aa7b commit a70f74b

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

cmd/common/json.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@ package common
22

33
import (
44
"context"
5+
"encoding/base64"
56
"encoding/json"
67
"fmt"
78
"strings"
89
"unicode/utf8"
910

11+
"github.com/spf13/cobra"
12+
1013
"github.com/oasisprotocol/oasis-core/go/common"
1114
coreSignature "github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
1215
consensusPretty "github.com/oasisprotocol/oasis-core/go/common/prettyprint"
1316
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
1417
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
1518
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
16-
"github.com/spf13/cobra"
1719

1820
"github.com/oasisprotocol/oasis-core/go/common/cbor"
1921
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/contracts"
22+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rofl"
2023
)
2124

2225
// PrettyJSONMarshal returns pretty-printed JSON encoding of v.
@@ -46,6 +49,40 @@ func JSONMarshalKey(k interface{}) (keyJSON []byte, err error) {
4649
return
4750
}
4851

52+
// JSONPrettyPrintRoflAppConfig is a wrapper around rofl.AppConfig that implements custom JSON marshaling.
53+
type JSONPrettyPrintRoflAppConfig rofl.AppConfig
54+
55+
// MarshalJSON implements custom JSON marshaling.
56+
func (a JSONPrettyPrintRoflAppConfig) MarshalJSON() ([]byte, error) {
57+
type alias JSONPrettyPrintRoflAppConfig
58+
out := struct {
59+
alias
60+
// Overrides alias field so JSON marshaler uses this base64 string.
61+
SEK string `json:"sek"`
62+
}{
63+
alias: alias(a),
64+
SEK: base64.StdEncoding.EncodeToString(a.SEK[:]),
65+
}
66+
return json.Marshal(out)
67+
}
68+
69+
// JSONPrettyPrintRoflRegistration is a wrapper around rofl.Registration that implements custom JSON marshaling.
70+
type JSONPrettyPrintRoflRegistration rofl.Registration
71+
72+
// MarshalJSON implements custom JSON marshaling.
73+
func (r JSONPrettyPrintRoflRegistration) MarshalJSON() ([]byte, error) {
74+
type alias JSONPrettyPrintRoflRegistration
75+
out := struct {
76+
alias
77+
// Overrides alias field so JSON marshaler uses this base64 string.
78+
REK string `json:"rek"`
79+
}{
80+
alias: alias(r),
81+
REK: base64.StdEncoding.EncodeToString(r.REK[:]),
82+
}
83+
return json.Marshal(out)
84+
}
85+
4986
// JSONPrintKeyValueTuple traverses potentially large number of items and prints JSON representation
5087
// of them.
5188
//

cmd/rofl/mgmt.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,17 @@ var (
452452

453453
if common.OutputFormat() == common.FormatJSON {
454454
output := struct {
455-
App *rofl.AppConfig `json:"app"`
456-
Replicas []*rofl.Registration `json:"replicas"`
455+
App common.JSONPrettyPrintRoflAppConfig `json:"app"`
456+
Replicas []common.JSONPrettyPrintRoflRegistration `json:"replicas"`
457457
}{
458-
App: appCfg,
459-
Replicas: replicas,
458+
App: common.JSONPrettyPrintRoflAppConfig(*appCfg),
459+
Replicas: make([]common.JSONPrettyPrintRoflRegistration, 0, len(replicas)),
460460
}
461+
// Convert replicas to pretty-printable format.
462+
for _, ai := range replicas {
463+
output.Replicas = append(output.Replicas, common.JSONPrettyPrintRoflRegistration(*ai))
464+
}
465+
461466
jsonOutput, err := common.PrettyJSONMarshal(output)
462467
cobra.CheckErr(err)
463468
fmt.Println(string(jsonOutput))

0 commit comments

Comments
 (0)