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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
// -
// ========================LICENSE_START=================================
// Copyright (C) 2024-2025: Deutsche Telekom
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// SPDX-License-Identifier: Apache-2.0
// ========================LICENSE_END===================================
// Package decision provides functionalities for handling decision requests using OPA (Open Policy Agent).
// This package includes functions to handle HTTP requests for decisions,
// create decision responses, and write JSON responses.
package decision
import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
openapi_types "github.com/oapi-codegen/runtime/types"
"github.com/open-policy-agent/opa/sdk"
"net/http"
"policy-opa-pdp/consts"
"policy-opa-pdp/pkg/log"
"policy-opa-pdp/pkg/metrics"
"policy-opa-pdp/pkg/model"
"policy-opa-pdp/pkg/model/oapicodegen"
"policy-opa-pdp/pkg/opasdk"
"policy-opa-pdp/pkg/pdpstate"
"policy-opa-pdp/pkg/policymap"
"policy-opa-pdp/pkg/utils"
"sort"
"strings"
)
// creates a response code map to ErrorResponseResponseCode
var httpToResponseCode = map[int]oapicodegen.ErrorResponseResponseCode{
400: oapicodegen.BadRequest,
401: oapicodegen.Unauthorized,
500: oapicodegen.InternalError,
}
// Gets responsecode from map
func getErrorResponseResponseCode(httpStatus int) oapicodegen.ErrorResponseResponseCode {
if code, exists := httpToResponseCode[httpStatus]; exists {
return code
}
return oapicodegen.InternalError
}
// writes a Successful JSON response to the HTTP response writer
func writeOpaJSONResponse(res http.ResponseWriter, status int, decisionRes oapicodegen.OPADecisionResponse) {
res.Header().Set("Content-Type", "application/json")
res.WriteHeader(status)
if err := json.NewEncoder(res).Encode(decisionRes); err != nil {
http.Error(res, err.Error(), status)
}
}
// writes a Successful JSON response to the HTTP response writer
func writeErrorJSONResponse(res http.ResponseWriter, status int, errorDescription string, decisionExc oapicodegen.ErrorResponse) {
res.Header().Set("Content-Type", "application/json")
res.WriteHeader(status)
if err := json.NewEncoder(res).Encode(decisionExc); err != nil {
http.Error(res, err.Error(), status)
}
}
// creates a success decision response
func createSuccessDecisionResponse(policyName string, output map[string]interface{}) *oapicodegen.OPADecisionResponse {
return &oapicodegen.OPADecisionResponse{
PolicyName: policyName,
Output: output,
}
}
// creates a decision response based on the provided parameters
func createSuccessDecisionResponseWithStatus(policyName string, output map[string]interface{}, statusMessage string) *oapicodegen.OPADecisionResponse {
return &oapicodegen.OPADecisionResponse{
PolicyName: policyName,
Output: output,
StatusMessage: &statusMessage,
}
}
// creates a decision response based on the provided parameters
func createDecisionExceptionResponse(statusCode int, errorMessage string, policyName string) *oapicodegen.ErrorResponse {
responseCode := getErrorResponseResponseCode(statusCode)
return &oapicodegen.ErrorResponse{
ResponseCode: (*oapicodegen.ErrorResponseResponseCode)(&responseCode),
ErrorMessage: &errorMessage,
PolicyName: &policyName,
}
}
// handles HTTP requests for decisions using OPA.
func OpaDecision(res http.ResponseWriter, req *http.Request) {
log.Debugf("PDP received a decision request.")
var errorDtls string
var httpStatus int
var policyId = ""
requestId, _ := processRequestHeaders(req, res)
log.Debugf("Headers processed for requestId: %s", requestId)
if !isSystemActive() {
errorDtls = "System Is In PASSIVE State, error handling request."
httpStatus = http.StatusInternalServerError
} else if req.Method != http.MethodPost {
errorDtls = req.Method + " MethodNotAllowed"
httpStatus = http.StatusMethodNotAllowed
} else {
handleDecisionRequest(res, req, &errorDtls, &httpStatus, &policyId)
}
if errorDtls != "" {
sendDecisionErrorResponse(errorDtls, res, httpStatus, policyId)
}
}
// Function to handle decision request logic
func handleDecisionRequest(res http.ResponseWriter, req *http.Request, errorDtls *string, httpStatus *int, policyId *string) {
decisionReq, err := parseRequestBody(req)
if err != nil {
*errorDtls = err.Error()
*httpStatus = http.StatusBadRequest
return
}
// Validate the request body
validationErrors := utils.ValidateOPADataRequest(decisionReq)
if decisionReq.PolicyFilter == nil || len(decisionReq.PolicyFilter) == 0 {
validationErrors = append(validationErrors, "PolicyFilter is required")
}
if len(validationErrors) > 0 {
*errorDtls = strings.Join(validationErrors, ", ")
log.Errorf("Facing validation error in requestbody - %s", *errorDtls)
*httpStatus = http.StatusBadRequest
return
}
log.Debugf("Validation successful for request fields")
// If validation passes, handle the decision request
decisionReq.PolicyName = strings.ReplaceAll(decisionReq.PolicyName, ".", "/")
handlePolicyValidation(res, decisionReq, errorDtls, httpStatus, policyId)
}
// Function to handle policy validation logic
func handlePolicyValidation(res http.ResponseWriter, decisionReq *oapicodegen.OPADecisionRequest, errorDtls *string, httpStatus *int, policyId *string) {
policiesMap := policymap.LastDeployedPolicies
if policiesMap == "" {
*errorDtls = "No policies are deployed."
*httpStatus = http.StatusBadRequest
return
}
extractedPolicies := policymap.ExtractDeployedPolicies(policiesMap)
if extractedPolicies == nil {
log.Warnf("No Policies extracted from Policy Map")
*errorDtls = "No policies are deployed."
*httpStatus = http.StatusBadRequest
return
}
if !policyExists(decisionReq.PolicyName, extractedPolicies) {
*errorDtls = fmt.Sprintf("Policy Name %s does not exist", decisionReq.PolicyName)
*httpStatus = http.StatusBadRequest
return
}
// Process OPA decision
opa, err := getOpaInstance()
if err != nil {
*errorDtls = "Failed to get OPA instance."
*httpStatus = http.StatusInternalServerError
*policyId = decisionReq.PolicyName
return
}
processOpaDecision(res, opa, decisionReq)
}
// Function to check if policy exists in extracted policies
func policyExists(policyName string, extractedPolicies []model.ToscaConceptIdentifier) bool {
for _, policy := range extractedPolicies {
if strings.ReplaceAll(policy.Name, ".", "/") == policyName {
return true
}
}
return false
}
// This function processes the request headers
func processRequestHeaders(req *http.Request, res http.ResponseWriter) (string, *oapicodegen.DecisionParams) {
requestId := req.Header.Get("X-ONAP-RequestID")
var parsedUUID *uuid.UUID
var decisionParams *oapicodegen.DecisionParams
if requestId != "" && utils.IsValidUUID(requestId) {
tempUUID, err := uuid.Parse(requestId)
if err == nil {
parsedUUID = &tempUUID
decisionParams = &oapicodegen.DecisionParams{
XONAPRequestID: (*openapi_types.UUID)(parsedUUID),
}
res.Header().Set("X-ONAP-RequestID", decisionParams.XONAPRequestID.String())
} else {
log.Warnf("Error Parsing the requestID: %v", err)
}
} else {
requestId = "Unknown"
res.Header().Set("X-ONAP-RequestID", requestId)
}
res.Header().Set("X-LatestVersion", consts.LatestVersion)
res.Header().Set("X-PatchVersion", consts.PatchVersion)
res.Header().Set("X-MinorVersion", consts.MinorVersion)
return requestId, decisionParams
}
// This returns whether the system is active or not
func isSystemActive() bool {
return pdpstate.GetCurrentState() == model.Active
}
// This method parses the body and checks whether it is properly formatted JSON or not
func parseRequestBody(req *http.Request) (*oapicodegen.OPADecisionRequest, error) {
var decisionReq oapicodegen.OPADecisionRequest
if err := json.NewDecoder(req.Body).Decode(&decisionReq); err != nil {
return nil, err
}
return &decisionReq, nil
}
// This function sends the error response
func sendDecisionErrorResponse(msg string, res http.ResponseWriter, httpStatus int, policyName string) {
log.Warnf("%s", msg)
decisionExc := createDecisionExceptionResponse(httpStatus, msg, policyName)
metrics.IncrementDecisionFailureCount()
metrics.IncrementTotalErrorCount()
writeErrorJSONResponse(res, httpStatus, msg, *decisionExc)
}
type OPASingletonInstanceFunc func() (*sdk.OPA, error)
var OPASingletonInstance OPASingletonInstanceFunc = opasdk.GetOPASingletonInstance
// This function returns the opasdk instance
func getOpaInstance() (*sdk.OPA, error) {
return OPASingletonInstance()
}
type OPADecisionFunc func(opa *sdk.OPA, ctx context.Context, options sdk.DecisionOptions) (*sdk.DecisionResult, error)
var OPADecision OPADecisionFunc = (*sdk.OPA).Decision
// This function processes the OPA decision
func processOpaDecision(res http.ResponseWriter, opa *sdk.OPA, decisionReq *oapicodegen.OPADecisionRequest) {
ctx := context.Background()
log.Debugf("SDK making a decision")
var decisionRes *oapicodegen.OPADecisionResponse
//OPA is seding success with a warning message if "input" parameter is missing, so we need to send success response
inputBytes, err := json.Marshal(decisionReq.Input)
if err != nil {
log.Warnf("Failed to unmarshal decision Request Input: %vg", err)
return
}
if inputBytes == nil || len(inputBytes) == 0 {
statusMessage := "{\"warning\":{\"code\":\"api_usage_warning\",\"message\":\"'input' key missing from the request\"}}"
decisionRes = createSuccessDecisionResponseWithStatus(decisionReq.PolicyName, nil, statusMessage)
} else {
options := sdk.DecisionOptions{Path: decisionReq.PolicyName, Input: decisionReq.Input}
decisionResult, decisionErr := OPADecision(opa, ctx, options)
jsonOutput, err := json.MarshalIndent(decisionResult, "", " ")
if err != nil {
log.Warnf("Error serializing decision output: %v\n", err)
return
}
log.Debugf("RAW opa Decision output:\n%s\n", string(jsonOutput))
//while making decision . is replaced by /. reverting back.
decisionReq.PolicyName = strings.ReplaceAll(decisionReq.PolicyName, "/", ".")
if decisionErr != nil {
sendDecisionErrorResponse(decisionErr.Error(), res, http.StatusInternalServerError, decisionReq.PolicyName)
return
}
var policyFilter []string
if decisionReq.PolicyFilter != nil {
policyFilter = decisionReq.PolicyFilter
}
result, _ := decisionResult.Result.(map[string]interface{})
outputMap, unmatchedFilters, validPolicyFilters := processPolicyFilter(result, policyFilter)
if len(unmatchedFilters) > 0 {
message := fmt.Sprintf("Policy Filter(s) not matching, Valid Filter(s) are: [%s]", strings.Join(validPolicyFilters, ", "))
decisionRes = createSuccessDecisionResponseWithStatus(decisionReq.PolicyName, outputMap, message)
} else {
decisionRes = createSuccessDecisionResponse(decisionReq.PolicyName, outputMap)
}
}
metrics.IncrementDecisionSuccessCount()
writeOpaJSONResponse(res, http.StatusOK, *decisionRes)
}
// This function processes the policy filters
func processPolicyFilter(result map[string]interface{}, policyFilter []string) (map[string]interface{}, []string, []string) {
if len(policyFilter) > 0 {
filteredResult, unmatchedFilters, validfilters := applyPolicyFilter(result, policyFilter)
if len(filteredResult) > 0 {
return filteredResult, unmatchedFilters, validfilters
}
}
return nil, policyFilter, getValidPolicyFilters(result)
}
// Get Valid Filters and collects unmatched filters
func applyPolicyFilter(result map[string]interface{}, filters []string) (map[string]interface{}, []string, []string) {
filteredOutput := make(map[string]interface{})
unmatchedFilters := []string{}
validFilters := getValidPolicyFilters(result)
for _, filter := range filters {
if filter == "" {
// when filter is "" empty, the entire resultant data will be reported
return result, nil, validFilters
}
// Try to find the value in the result map
if value := findNestedValue(result, strings.Split(filter, "/")); value != nil {
filteredOutput[filter] = value // Store using full path
} else if value, exists := result[filter]; exists {
// Allow direct key match (for non-nested filters)
filteredOutput[filter] = value
} else {
unmatchedFilters = append(unmatchedFilters, filter) // Collect unmatched filters
}
}
return filteredOutput, unmatchedFilters, validFilters
}
// handles the nested Policy Filters available when multiple rego files are included.
func findNestedValue(opaSdkResult map[string]interface{}, keys []string) interface{} {
if len(keys) == 0 {
return nil
}
currentMap := opaSdkResult
for _, key := range keys {
value, exists := currentMap[key]
if !exists {
return nil // Key doesn't exist
}
// If it's a nested map, continue traversal
if nextNestedMap, ok := value.(map[string]interface{}); ok {
currentMap = nextNestedMap
} else {
return value // Return final value (non-map)
}
}
return currentMap
}
// returns the valid Policy Filters available
func getValidPolicyFilters(opaSdkResult map[string]interface{}) []string {
keys := make([]string, 0)
for k, v := range opaSdkResult {
keys = append(keys, k)
if nestedMap, ok := v.(map[string]interface{}); ok {
for nestedKey := range nestedMap {
keys = append(keys, k+"/"+nestedKey)
}
}
}
sort.Strings(keys)
return keys
}
|