-
Notifications
You must be signed in to change notification settings - Fork 276
/
canonicalstring.go
101 lines (90 loc) · 2.98 KB
/
canonicalstring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright The Athenz Authors
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package util
import (
"encoding/json"
"fmt"
"reflect"
"github.com/ardielle/ardielle-go/rdl"
)
type DomainSignedPolicyData struct {
KeyId string `json:"keyId"`
Signature string `json:"signature"`
SignedPolicyData *SignedPolicyData `json:"signedPolicyData"`
}
type SignedPolicyData struct {
Expires *rdl.Timestamp `json:"expires"`
Modified *rdl.Timestamp `json:"modified"`
PolicyData *PolicyData `json:"policyData"`
ZmsKeyId string `json:"zmsKeyId"`
ZmsSignature string `json:"zmsSignature"`
}
type PolicyData struct {
Domain string `json:"domain,omitempty"`
Policies []*Policy `json:"policies,omitempty"`
}
type Policy struct {
Assertions []*Assertion `json:"assertions,omitempty"`
Modified *rdl.Timestamp `json:"modified,omitempty"`
Name string `json:"name,omitempty"`
}
type Assertion struct {
Action string `json:"action,omitempty"`
Effect string `json:"effect,omitempty"`
Id int64 `json:"id,omitempty"`
Resource string `json:"resource,omitempty"`
Role string `json:"role,omitempty"`
}
type AssertionEffect struct {
value string
}
func ToCanonicalString(obj interface{}) (string, error) {
t := reflect.TypeOf(obj).String()
j, err := json.Marshal(obj)
if err != nil {
return "", fmt.Errorf("Failed to Marshal Json for converting to canonical form, Error:%v", err)
}
switch t {
case "*zts.SignedPolicyData":
{
var signedPolicyData *SignedPolicyData
err := json.Unmarshal(j, &signedPolicyData)
if err != nil {
return "", fmt.Errorf("Failed to Unmarshal Json for converting signed policy data to canonical form, Error:%v", err)
}
canonicalStr, err := json.Marshal(signedPolicyData)
if err != nil {
return "", fmt.Errorf("Failed to Marshal Json for converting signed policy data to canonical form, Error:%v", err)
}
return string(canonicalStr), nil
}
case "*zts.PolicyData":
{
var policyData *PolicyData
err := json.Unmarshal(j, &policyData)
if err != nil {
return "", fmt.Errorf("Failed to Unmarshal Json for converting policy data to canonical form, Error:%v", err)
}
canonicalStr, err := json.Marshal(policyData)
if err != nil {
return "", fmt.Errorf("Failed to Marshal Json for converting policy data to canonical form, Error:%v", err)
}
return string(canonicalStr), nil
}
case "*zts.Policy":
{
var policy *Policy
err := json.Unmarshal(j, &policy)
if err != nil {
return "", fmt.Errorf("Failed to Unmarshal Json for converting policies to canonical form, Error:%v", err)
}
canonicalStr, err := json.Marshal(policy)
if err != nil {
return "", fmt.Errorf("Failed to Marshal Json for converting policies to canonical form, Error:%v", err)
}
return string(canonicalStr), nil
}
default:
return "", fmt.Errorf("Unrecognized input for converting to Canonical form")
}
}