| 1 |
package trustgraph |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/x509" |
| 5 |
"encoding/json" |
| 6 |
"io" |
| 7 |
"io/ioutil" |
| 8 |
"sort" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/containers/libtrust" |
| 13 |
) |
| 14 |
|
| 15 |
type jsonGrant struct { |
| 16 |
Subject string `json:"subject"` |
| 17 |
Permission uint16 `json:"permission"` |
| 18 |
Grantee string `json:"grantee"` |
| 19 |
} |
| 20 |
|
| 21 |
type jsonRevocation struct { |
| 22 |
Subject string `json:"subject"` |
| 23 |
Revocation uint16 `json:"revocation"` |
| 24 |
Grantee string `json:"grantee"` |
| 25 |
} |
| 26 |
|
| 27 |
type jsonStatement struct { |
| 28 |
Revocations []*jsonRevocation `json:"revocations"` |
| 29 |
Grants []*jsonGrant `json:"grants"` |
| 30 |
Expiration time.Time `json:"expiration"` |
| 31 |
IssuedAt time.Time `json:"issuedAt"` |
| 32 |
} |
| 33 |
|
| 34 |
func (g *jsonGrant) Grant(statement *Statement) *Grant { |
| 35 |
return &Grant{ |
| 36 |
Subject: g.Subject, |
| 37 |
Permission: g.Permission, |
| 38 |
Grantee: g.Grantee, |
| 39 |
statement: statement, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
type Statement struct { |
| 47 |
jsonStatement |
| 48 |
|
| 49 |
signature *libtrust.JSONSignature |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func (s *Statement) IsExpired() bool { |
| 54 |
return s.Expiration.Before(time.Now().Add(-10 * time.Second)) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
func (s *Statement) Bytes() ([]byte, error) { |
| 61 |
return s.signature.PrettySignature("signatures") |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
func LoadStatement(r io.Reader, authority *x509.CertPool) (*Statement, error) { |
| 66 |
b, err := ioutil.ReadAll(r) |
| 67 |
if err != nil { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
js, err := libtrust.ParsePrettySignature(b, "signatures") |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
payload, err := js.Payload() |
| 75 |
if err != nil { |
| 76 |
return nil, err |
| 77 |
} |
| 78 |
var statement Statement |
| 79 |
err = json.Unmarshal(payload, &statement.jsonStatement) |
| 80 |
if err != nil { |
| 81 |
return nil, err |
| 82 |
} |
| 83 |
|
| 84 |
if authority == nil { |
| 85 |
_, err = js.Verify() |
| 86 |
if err != nil { |
| 87 |
return nil, err |
| 88 |
} |
| 89 |
} else { |
| 90 |
_, err = js.VerifyChains(authority) |
| 91 |
if err != nil { |
| 92 |
return nil, err |
| 93 |
} |
| 94 |
} |
| 95 |
statement.signature = js |
| 96 |
|
| 97 |
return &statement, nil |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
func CreateStatement(grants, revocations io.Reader, expiration time.Duration, key libtrust.PrivateKey, chain []*x509.Certificate) (*Statement, error) { |
| 103 |
var statement Statement |
| 104 |
err := json.NewDecoder(grants).Decode(&statement.jsonStatement.Grants) |
| 105 |
if err != nil { |
| 106 |
return nil, err |
| 107 |
} |
| 108 |
err = json.NewDecoder(revocations).Decode(&statement.jsonStatement.Revocations) |
| 109 |
if err != nil { |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
statement.jsonStatement.Expiration = time.Now().UTC().Add(expiration) |
| 113 |
statement.jsonStatement.IssuedAt = time.Now().UTC() |
| 114 |
|
| 115 |
b, err := json.MarshalIndent(&statement.jsonStatement, "", " ") |
| 116 |
if err != nil { |
| 117 |
return nil, err |
| 118 |
} |
| 119 |
|
| 120 |
statement.signature, err = libtrust.NewJSONSignature(b) |
| 121 |
if err != nil { |
| 122 |
return nil, err |
| 123 |
} |
| 124 |
err = statement.signature.SignWithChain(key, chain) |
| 125 |
if err != nil { |
| 126 |
return nil, err |
| 127 |
} |
| 128 |
|
| 129 |
return &statement, nil |
| 130 |
} |
| 131 |
|
| 132 |
type statementList []*Statement |
| 133 |
|
| 134 |
func (s statementList) Len() int { |
| 135 |
return len(s) |
| 136 |
} |
| 137 |
|
| 138 |
func (s statementList) Less(i, j int) bool { |
| 139 |
return s[i].IssuedAt.Before(s[j].IssuedAt) |
| 140 |
} |
| 141 |
|
| 142 |
func (s statementList) Swap(i, j int) { |
| 143 |
s[i], s[j] = s[j], s[i] |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
func CollapseStatements(statements []*Statement, useExpired bool) ([]*Grant, time.Time, error) { |
| 149 |
sorted := make(statementList, 0, len(statements)) |
| 150 |
for _, statement := range statements { |
| 151 |
if useExpired || !statement.IsExpired() { |
| 152 |
sorted = append(sorted, statement) |
| 153 |
} |
| 154 |
} |
| 155 |
sort.Sort(sorted) |
| 156 |
|
| 157 |
var minExpired time.Time |
| 158 |
var grantCount int |
| 159 |
roots := map[string]*grantNode{} |
| 160 |
for i, statement := range sorted { |
| 161 |
if statement.Expiration.Before(minExpired) || i == 0 { |
| 162 |
minExpired = statement.Expiration |
| 163 |
} |
| 164 |
for _, grant := range statement.Grants { |
| 165 |
parts := strings.Split(grant.Grantee, "/") |
| 166 |
nodes := roots |
| 167 |
g := grant.Grant(statement) |
| 168 |
grantCount = grantCount + 1 |
| 169 |
|
| 170 |
for _, part := range parts { |
| 171 |
node, nodeOk := nodes[part] |
| 172 |
if !nodeOk { |
| 173 |
node = newGrantNode() |
| 174 |
nodes[part] = node |
| 175 |
} |
| 176 |
node.grants = append(node.grants, g) |
| 177 |
nodes = node.children |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
for _, revocation := range statement.Revocations { |
| 182 |
parts := strings.Split(revocation.Grantee, "/") |
| 183 |
nodes := roots |
| 184 |
|
| 185 |
var node *grantNode |
| 186 |
var nodeOk bool |
| 187 |
for _, part := range parts { |
| 188 |
node, nodeOk = nodes[part] |
| 189 |
if !nodeOk { |
| 190 |
break |
| 191 |
} |
| 192 |
nodes = node.children |
| 193 |
} |
| 194 |
if node != nil { |
| 195 |
for _, grant := range node.grants { |
| 196 |
if isSubName(grant.Subject, revocation.Subject) { |
| 197 |
grant.Permission = grant.Permission &^ revocation.Revocation |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
retGrants := make([]*Grant, 0, grantCount) |
| 205 |
for _, rootNodes := range roots { |
| 206 |
retGrants = append(retGrants, rootNodes.grants...) |
| 207 |
} |
| 208 |
|
| 209 |
return retGrants, minExpired, nil |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
func FilterStatements(grants []*Grant) ([]*Statement, error) { |
| 214 |
statements := map[*Statement]bool{} |
| 215 |
for _, grant := range grants { |
| 216 |
if grant.statement != nil { |
| 217 |
statements[grant.statement] = true |
| 218 |
} |
| 219 |
} |
| 220 |
retStatements := make([]*Statement, len(statements)) |
| 221 |
var i int |
| 222 |
for statement := range statements { |
| 223 |
retStatements[i] = statement |
| 224 |
i++ |
| 225 |
} |
| 226 |
return retStatements, nil |
| 227 |
} |