From 9f7690327f8d74abdd86232546a154ab8408d174 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Tue, 16 Mar 2021 00:26:07 +0100 Subject: started to re-add basic client commands --- descriptor/descriptor.go | 58 ---------------------- descriptor/descriptor_test.go | 109 ------------------------------------------ descriptor/stfe.json | 12 ----- 3 files changed, 179 deletions(-) delete mode 100644 descriptor/descriptor.go delete mode 100644 descriptor/descriptor_test.go delete mode 100644 descriptor/stfe.json (limited to 'descriptor') diff --git a/descriptor/descriptor.go b/descriptor/descriptor.go deleted file mode 100644 index efe2cf1..0000000 --- a/descriptor/descriptor.go +++ /dev/null @@ -1,58 +0,0 @@ -package descriptor - -import ( - "bytes" - "fmt" - - "encoding/base64" - "encoding/json" - "io/ioutil" - - "github.com/system-transparency/stfe/namespace" -) - -// Operator is an stfe log operator that runs zero or more logs -type Operator struct { - Name string `json:"name"` - Email string `json:"email"` - Logs []*Log `json:"logs"` -} - -// Log is a collection of immutable stfe log parameters -type Log struct { - Id []byte `json:"id"` // Serialized namespace - BaseUrl string `json:"base_url"` // E.g., example.com/st/v1 - // TODO: List of supported namespace types? -} - -func FindLog(ops []Operator, logId []byte) (*Log, error) { - for _, op := range ops { - for _, log := range op.Logs { - if bytes.Equal(logId, log.Id) { - return log, nil - } - } - } - return nil, fmt.Errorf("no such log: %s", base64.StdEncoding.EncodeToString(logId)) -} - -// LoadOperators loads a list of json-encoded log operators from a given path -func LoadOperators(path string) ([]Operator, error) { - blob, err := ioutil.ReadFile(path) - if err != nil { - return nil, fmt.Errorf("failed reading log operators: %v", err) - } - var ops []Operator - if err := json.Unmarshal(blob, &ops); err != nil { - return nil, fmt.Errorf("failed decoding log operators: %v", err) - } - return ops, nil -} - -func (l *Log) Namespace() (*namespace.Namespace, error) { - var n namespace.Namespace - if err := n.Unmarshal(l.Id); err != nil { - return nil, fmt.Errorf("invalid namespace: %v", err) - } - return &n, nil -} diff --git a/descriptor/descriptor_test.go b/descriptor/descriptor_test.go deleted file mode 100644 index 22641ca..0000000 --- a/descriptor/descriptor_test.go +++ /dev/null @@ -1,109 +0,0 @@ -package descriptor - -import ( - "fmt" - "testing" - - "encoding/base64" - "encoding/json" -) - -const ( - operatorListJson = `[{"name":"Test operator","email":"test@example.com","logs":[{"id":"AAEgFKl1V+J3ib3Aav86UgGD7GRRtcKIdDhgc0G4vVD/TGc=","base_url":"example.com/st/v1"}]}]` -) - -func TestMarshal(t *testing.T) { - for _, table := range []struct { - in []Operator - want string - }{ - {makeOperatorList(), operatorListJson}, - } { - b, err := json.Marshal(table.in) - if err != nil { - t.Errorf("operator list marshaling failed: %v", err) - } - if string(b) != table.want { - t.Errorf("\nwant %s\n got %s", table.want, string(b)) - } - } - -} - -func TestUnmarshal(t *testing.T) { - for _, table := range []struct { - in []byte - want error - }{ - {[]byte(operatorListJson), nil}, - } { - var op []Operator - if err := json.Unmarshal(table.in, &op); err != table.want { - t.Errorf("wanted err=%v, got %v", table.want, err) - } - } -} - -func TestFindLog(t *testing.T) { - for _, table := range []struct { - ops []Operator - logId []byte - wantError bool - }{ - {makeOperatorList(), deb64("AAEgFKl1V+J3ib3Aav86UgGD7GRRtcKIdDhgc0G4vVD/TGc="), false}, - {makeOperatorList(), []byte{0, 1, 2, 3}, true}, - } { - _, err := FindLog(table.ops, table.logId) - if (err != nil) != table.wantError { - t.Errorf("wanted log not found for id: %v", table.logId) - } - } -} - -func TestNamespace(t *testing.T) { - for _, table := range []struct { - description string - id []byte - wantErr bool - }{ - { - description: "invalid: not a namespace", - id: []byte{0,1,2,3}, - wantErr: true, - }, - { - description: "valid", - id: deb64("AAEgFKl1V+J3ib3Aav86UgGD7GRRtcKIdDhgc0G4vVD/TGc="), - }, - }{ - l := &Log{ Id: table.id, BaseUrl: "example.com/st/v1" } - _, err := l.Namespace() - if got, want := err != nil, table.wantErr; got != want { - t.Errorf("wanted error %v but got %v in test %q: %v", got, want, table.description, err) - return - } - } -} - -func makeOperatorList() []Operator { - return []Operator{ - Operator{ - Name: "Test operator", - Email: "test@example.com", - Logs: []*Log{ - &Log{ - Id: deb64("AAEgFKl1V+J3ib3Aav86UgGD7GRRtcKIdDhgc0G4vVD/TGc="), - BaseUrl: "example.com/st/v1", - }, - }, - }, - } -} - -func deb64(s string) []byte { - b, err := base64.StdEncoding.DecodeString(s) - if err != nil { - panic(fmt.Sprintf("failed decoding base64: %v", err)) - } - return b -} diff --git a/descriptor/stfe.json b/descriptor/stfe.json deleted file mode 100644 index 34f884b..0000000 --- a/descriptor/stfe.json +++ /dev/null @@ -1,12 +0,0 @@ -[ - { - "name": "Test operator", - "email":"test@example.com", - "logs": [ - { - "id":"AAEgFKl1V+J3ib3Aav86UgGD7GRRtcKIdDhgc0G4vVD/TGc=", - "base_url":"localhost:6965/st/v1" - } - ] - } -] -- cgit v1.2.3