Skip to content

Commit

Permalink
get: add --attestation flag
Browse files Browse the repository at this point in the history
  • Loading branch information
makew0rld committed Jul 3, 2024
1 parent 0bdfd70 commit d18c5e6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
35 changes: 18 additions & 17 deletions aa/aa.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,29 @@ type GetAttOpts struct {
// Attestation as stored in the database in DAG-CBOR.
// See https://github.com/starlinglab/authenticated-attributes/blob/main/docs/database.md#schema
//
// This does not encode into the same CBOR it was decoded from, but that's okay
// as encoding this struct should be not required anywhere.
// This may not encode into the same CBOR it was decoded from, but that's okay
// as that should not be required anywhere.
type AttEntry struct {
Signature struct {
PubKey [32]byte
Sig [64]byte
Msg CborCID
}
PubKey []byte `json:"pubKey"`
Sig []byte `json:"sig"`
Msg CborCID `json:"msg"`
} `json:"signature"`
Timestamp struct {
OTS struct {
Proof []byte
Upgraded bool
Msg CborCID
}
}
Proof []byte `json:"proof"`
Upgraded bool `json:"upgraded"`
Msg CborCID `json:"msg"`
} `json:"ots"`
} `json:"timestamp"`
Attestation struct {
CID CborCID
Value any
Encrypted bool
Timestamp time.Time
}
Version string
CID CborCID `json:"CID"`
Value any `json:"value"`
Attribute string `json:"attribute"`
Encrypted bool `json:"encrypted"`
Timestamp time.Time `json:"timestamp"`
} `json:"attestation"`
Version string `json:"version"`
}

// Attributes for uploading.
Expand Down
30 changes: 25 additions & 5 deletions get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
)

var (
attr string
getAll bool
isEncrypted bool
encKeyPath string
attr string
getAll bool
isEncrypted bool
encKeyPath string
showAttestation bool
)

func Run(args []string) error {
Expand All @@ -25,6 +26,7 @@ func Run(args []string) error {
fs.BoolVar(&getAll, "all", false, "get all attributes instead of just one")
fs.BoolVar(&isEncrypted, "encrypted", false, "value to get is encrypted")
fs.StringVar(&encKeyPath, "key", "", "(optional) manual path to encryption key file, implies --encrypted")
fs.BoolVar(&showAttestation, "attestation", false, "show attestation information, not just value. Note values are not decrypted for this output.")

err := fs.Parse(args)
if err != nil {
Expand All @@ -37,6 +39,9 @@ func Run(args []string) error {
fs.PrintDefaults()
return fmt.Errorf("\nprovide attribute name with --attr")
}
if getAll && showAttestation {
return fmt.Errorf("can't use --all and --attestation together")
}
if fs.NArg() != 1 {
return fmt.Errorf("provide a single CID to work with")
}
Expand Down Expand Up @@ -82,14 +87,29 @@ func Run(args []string) error {
os.Stdout.Write(b)
fmt.Fprintln(os.Stderr, "\n\nThis is not an exact canonical representation.")
} else {
ae, err := aa.GetAttestation(cid, attr, aa.GetAttOpts{EncKey: encKey})
leaveEnc := false
if showAttestation {
leaveEnc = true
}

ae, err := aa.GetAttestation(cid, attr, aa.GetAttOpts{EncKey: encKey, LeaveEncrypted: leaveEnc})
if err == aa.ErrNeedsKey {
return fmt.Errorf("error attestation is encrypted, use --encrypted or --key")
}
if err != nil {
return fmt.Errorf("error getting attestation: %w", err)
}

if showAttestation {
b, err := json.MarshalIndent(ae, "", " ")
if err != nil {
return fmt.Errorf("error encoding value as JSON: %w", err)
}
os.Stdout.Write(b)
fmt.Fprintln(os.Stderr, "\n\nThis is not an exact canonical representation.")
return nil
}

kind := reflect.TypeOf(ae.Attestation.Value).Kind()
if kind == reflect.Slice || kind == reflect.Struct || kind == reflect.Map ||
kind == reflect.Array {
Expand Down

0 comments on commit d18c5e6

Please sign in to comment.