forked from snapcore/snapd
/
api_asserts.go
146 lines (129 loc) · 3.87 KB
/
api_asserts.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package daemon
import (
"errors"
"net/http"
"net/url"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/auth"
)
var (
// TODO: allow to post assertions for UserOK? they are verified anyway
assertsCmd = &Command{
Path: "/v2/assertions",
UserOK: true,
GET: getAssertTypeNames,
POST: doAssert,
}
assertsFindManyCmd = &Command{
Path: "/v2/assertions/{assertType}",
UserOK: true,
GET: assertsFindMany,
}
)
// a helper type for parsing the options specified to /v2/assertions and other
// such endpoints that can either do JSON or assertion depending on the value
// of the the URL query parameters
type daemonAssertFormatOptions struct {
jsonResult bool
headersOnly bool
headers map[string]string
}
// helper for parsing url query options into formatting option vars
func parseHeadersFormatOptionsFromURL(q url.Values) (*daemonAssertFormatOptions, error) {
res := daemonAssertFormatOptions{}
res.headers = make(map[string]string)
for k := range q {
if k == "json" {
switch q.Get(k) {
case "false":
res.jsonResult = false
case "headers":
res.headersOnly = true
fallthrough
case "true":
res.jsonResult = true
default:
return nil, errors.New(`"json" query parameter when used must be set to "true" or "headers"`)
}
continue
}
res.headers[k] = q.Get(k)
}
return &res, nil
}
func getAssertTypeNames(c *Command, r *http.Request, user *auth.UserState) Response {
return SyncResponse(map[string][]string{
"types": asserts.TypeNames(),
}, nil)
}
func doAssert(c *Command, r *http.Request, user *auth.UserState) Response {
batch := asserts.NewBatch(nil)
_, err := batch.AddStream(r.Body)
if err != nil {
return BadRequest("cannot decode request body into assertions: %v", err)
}
state := c.d.overlord.State()
state.Lock()
defer state.Unlock()
if err := assertstate.AddBatch(state, batch, &asserts.CommitOptions{
Precheck: true,
}); err != nil {
return BadRequest("assert failed: %v", err)
}
// TODO: what more info do we want to return on success?
return &resp{
Type: ResponseTypeSync,
Status: 200,
}
}
func assertsFindMany(c *Command, r *http.Request, user *auth.UserState) Response {
assertTypeName := muxVars(r)["assertType"]
assertType := asserts.Type(assertTypeName)
if assertType == nil {
return BadRequest("invalid assert type: %q", assertTypeName)
}
opts, err := parseHeadersFormatOptionsFromURL(r.URL.Query())
if err != nil {
return BadRequest(err.Error())
}
state := c.d.overlord.State()
state.Lock()
db := assertstate.DB(state)
state.Unlock()
assertions, err := db.FindMany(assertType, opts.headers)
if err != nil && !asserts.IsNotFound(err) {
return InternalError("searching assertions failed: %v", err)
}
if opts.jsonResult {
assertsJSON := make([]struct {
Headers map[string]interface{} `json:"headers,omitempty"`
Body string `json:"body,omitempty"`
}, len(assertions))
for i := range assertions {
assertsJSON[i].Headers = assertions[i].Headers()
if !opts.headersOnly {
assertsJSON[i].Body = string(assertions[i].Body())
}
}
return SyncResponse(assertsJSON, nil)
}
return AssertResponse(assertions, true)
}