Skip to content

Commit 20d4232

Browse files
committed
Enable sub-attesters content type selection
Now users may pick the desired output content type of each sub-attester by specifying field "content-type" in "attester-selection" as shown in the following example: "attester-selection": { mocktsm:{ "content-type": "application/vnd.veraison.configfs-tsm+json", "privilege_level": 3 } } Signed-off-by: Ian Chin Wang <[email protected]>
1 parent 8b2690f commit 20d4232

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

api/server.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,53 @@ func (s *Server) RatsdChares(w http.ResponseWriter, r *http.Request, param Ratsd
147147
return
148148
}
149149

150+
outputCt := formatOut.Formats[0].ContentType
150151
params, hasOption := options[pn]
151152
if !hasOption {
152153
params = json.RawMessage{}
154+
} else {
155+
attesterOptions := make(map[string]string)
156+
if err := json.Unmarshal(params, &attesterOptions); err != nil {
157+
errMsg := fmt.Sprintf(
158+
"failed to parse options for %s: %v", pn, err)
159+
p := &problems.DefaultProblem{
160+
Type: string(TagGithubCom2024VeraisonratsdErrorInvalidrequest),
161+
Title: string(InvalidRequest),
162+
Detail: errMsg,
163+
Status: http.StatusBadRequest,
164+
}
165+
s.reportProblem(w, p)
166+
return
167+
}
168+
169+
validCt := false
170+
if desiredCt, ok := attesterOptions["content-type"]; ok {
171+
for _, f := range formatOut.Formats {
172+
if f.ContentType == desiredCt {
173+
outputCt = desiredCt
174+
validCt = true
175+
break
176+
}
177+
}
178+
179+
if !validCt {
180+
errMsg := fmt.Sprintf(
181+
"%s does not support content type %s", pn, desiredCt)
182+
p := &problems.DefaultProblem{
183+
Type: string(TagGithubCom2024VeraisonratsdErrorInvalidrequest),
184+
Title: string(InvalidRequest),
185+
Detail: errMsg,
186+
Status: http.StatusBadRequest,
187+
}
188+
s.reportProblem(w, p)
189+
return
190+
}
191+
}
153192
}
154193

155-
s.logger.Info(pn, " output content type: ", formatOut.Formats[0].ContentType)
194+
s.logger.Info(pn, " output content type: ", outputCt)
156195
in := &compositor.EvidenceIn{
157-
ContentType: formatOut.Formats[0].ContentType,
196+
ContentType: outputCt,
158197
Nonce: nonce,
159198
Options: params,
160199
}

api/server_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ func TestRatsdChares_invalid_body(t *testing.T) {
103103
"attester-selection": "attester-slection"}`, validNonce),
104104
"failed to parse attester selection: json: cannot unmarshal string into" +
105105
` Go value of type map[string]json.RawMessage`},
106+
{"invalid attester options",
107+
fmt.Sprintf(`{"nonce": "%s",
108+
"attester-selection": {"mock-tsm":"invalid"}}`, validNonce),
109+
"failed to parse options for mock-tsm: json: cannot unmarshal string into" +
110+
` Go value of type map[string]string`},
111+
{"request content type unavailable",
112+
fmt.Sprintf(`{"nonce": "%s",
113+
"attester-selection": {"mock-tsm":{"content-type":"invalid"}}}`, validNonce),
114+
"mock-tsm does not support content type invalid"},
106115
}
107116

108117
for _, tt := range tests {

0 commit comments

Comments
 (0)