Skip to content

Commit

Permalink
added metafile flag for policy decisions (#757)
Browse files Browse the repository at this point in the history
* stripped name from Policy CLI commands
* update policy agent and remove name from policy apis
* added metafile flag for policy decisions
  • Loading branch information
davidmdm authored Jul 22, 2022
1 parent 52fb209 commit 571ae88
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 195 deletions.
7 changes: 3 additions & 4 deletions api/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func (c Client) ListPolicies(ownerID string) (interface{}, error) {

// CreationRequest represents the json payload to create a Policy in the Policy-Service
type CreationRequest struct {
Name string `json:"name"`
Context string `json:"context"`
Content string `json:"content"`
}
Expand Down Expand Up @@ -106,7 +105,6 @@ func (c Client) CreatePolicy(ownerID string, policy CreationRequest) (interface{
}

type UpdateRequest struct {
Name *string `json:"name,omitempty"`
Context *string `json:"context,omitempty"`
Content *string `json:"content,omitempty"`
}
Expand Down Expand Up @@ -271,8 +269,9 @@ func (c Client) GetDecisionLogs(ownerID string, request DecisionQueryRequest) ([
// DecisionRequest represents a request to Policy-Service to evaluate a given input against an organization's policies.
// The context determines which policies to apply.
type DecisionRequest struct {
Input string `json:"input"`
Context string `json:"context"`
Input string `json:"input"`
Context string `json:"context"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}

// MakeDecision sends a requests to Policy-Service public decision endpoint and returns the decision response
Expand Down
50 changes: 22 additions & 28 deletions api/policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestClientListPolicies(t *testing.T) {

t.Run("List Policies - Forbidden", func(t *testing.T) {
expectedResponse := `{"error": "Forbidden"}`
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand All @@ -59,7 +59,7 @@ func TestClientListPolicies(t *testing.T) {

t.Run("List Policies - Bad error json", func(t *testing.T) {
expectedResponse := `{"this is bad json": }`
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand All @@ -80,7 +80,7 @@ func TestClientListPolicies(t *testing.T) {
var expectedResponseValue interface{}
assert.NilError(t, json.Unmarshal([]byte(expectedResponse), &expectedResponseValue))

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
}))
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestClientListPolicies(t *testing.T) {
var expectedResponseValue interface{}
assert.NilError(t, json.Unmarshal([]byte(expectedResponse), &expectedResponseValue))

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
}))
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestClientGetPolicy(t *testing.T) {
t.Run("Get Policy - Bad Request", func(t *testing.T) {
expectedResponse := `{"error": "PolicyID: must be a valid UUID."}`

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand All @@ -177,7 +177,7 @@ func TestClientGetPolicy(t *testing.T) {

t.Run("Get Policy - Forbidden", func(t *testing.T) {
expectedResponse := `{"error": "Forbidden"}`
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand All @@ -194,7 +194,7 @@ func TestClientGetPolicy(t *testing.T) {

t.Run("Get Policy - Not Found", func(t *testing.T) {
expectedResponse := `{"error": "policy not found"}`
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand Down Expand Up @@ -224,7 +224,7 @@ func TestClientGetPolicy(t *testing.T) {
var expectedResponseValue interface{}
assert.NilError(t, json.Unmarshal([]byte(expectedResponse), &expectedResponseValue))

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
}))
Expand All @@ -242,7 +242,6 @@ func TestClientGetPolicy(t *testing.T) {
func TestClientCreatePolicy(t *testing.T) {
t.Run("expected request", func(t *testing.T) {
req := CreationRequest{
Name: "test-name",
Context: "config",
Content: "test-content",
}
Expand Down Expand Up @@ -276,7 +275,7 @@ func TestClientCreatePolicy(t *testing.T) {

t.Run("unexpected status code", func(t *testing.T) {
expectedResponse := `{"error": "Forbidden"}`
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand Down Expand Up @@ -317,7 +316,7 @@ func TestClientDeletePolicy(t *testing.T) {
t.Run("Delete Policy - Bad Request", func(t *testing.T) {
expectedResponse := `{"error": "PolicyID: must be a valid UUID."}`

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand All @@ -333,7 +332,7 @@ func TestClientDeletePolicy(t *testing.T) {

t.Run("Delete Policy - Forbidden", func(t *testing.T) {
expectedResponse := `{"error": "Forbidden"}`
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand All @@ -349,7 +348,7 @@ func TestClientDeletePolicy(t *testing.T) {

t.Run("Delete Policy - Not Found", func(t *testing.T) {
expectedResponse := `{"error": "policy not found"}`
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand All @@ -364,7 +363,7 @@ func TestClientDeletePolicy(t *testing.T) {
})

t.Run("Delete Policy - successfully deletes a policy", func(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNoContent)
}))
defer svr.Close()
Expand All @@ -379,11 +378,9 @@ func TestClientDeletePolicy(t *testing.T) {

func TestClientUpdatePolicy(t *testing.T) {
t.Run("expected request", func(t *testing.T) {
name := "test-name"
context := "config"
content := "test-content"
req := UpdateRequest{
Name: &name,
Context: &context,
Content: &content,
}
Expand Down Expand Up @@ -417,7 +414,7 @@ func TestClientUpdatePolicy(t *testing.T) {

t.Run("unexpected status code", func(t *testing.T) {
expectedResponse := `{"error": "Forbidden"}`
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand Down Expand Up @@ -463,10 +460,7 @@ func TestClientUpdatePolicy(t *testing.T) {
})

t.Run("one change", func(t *testing.T) {
name := "test-name"
req := UpdateRequest{
Name: &name,
}
req := UpdateRequest{}

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Header.Get("circle-token"), "testtoken")
Expand Down Expand Up @@ -591,7 +585,7 @@ func TestClientGetDecisionLogs(t *testing.T) {
t.Run("Get Decision Logs - Bad Request", func(t *testing.T) {
expectedResponse := `{"error": "Offset: must be an integer number."}`

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand All @@ -608,7 +602,7 @@ func TestClientGetDecisionLogs(t *testing.T) {

t.Run("Get Decision Logs - Forbidden", func(t *testing.T) {
expectedResponse := `{"error": "Forbidden"}`
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
Expand All @@ -629,7 +623,7 @@ func TestClientGetDecisionLogs(t *testing.T) {
var expectedResponseValue []interface{}
assert.NilError(t, json.Unmarshal([]byte(expectedResponse), &expectedResponseValue))

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
}))
Expand Down Expand Up @@ -676,7 +670,7 @@ func TestClientGetDecisionLogs(t *testing.T) {
var expectedResponseValue []interface{}
assert.NilError(t, json.Unmarshal([]byte(expectedResponse), &expectedResponseValue))

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte(expectedResponse))
assert.NilError(t, err)
}))
Expand Down Expand Up @@ -728,7 +722,7 @@ func TestMakeDecision(t *testing.T) {
Name: "unexpected statuscode",
OwnerID: "test-owner",
Request: DecisionRequest{},
Handler: func(w http.ResponseWriter, r *http.Request) {
Handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(400)
_, _ = io.WriteString(w, `{"error":"that was a bad request!"}`)
},
Expand All @@ -739,7 +733,7 @@ func TestMakeDecision(t *testing.T) {
Name: "unexpected statuscode no body",
OwnerID: "test-owner",
Request: DecisionRequest{},
Handler: func(w http.ResponseWriter, r *http.Request) {
Handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(204)
},
ExpectedError: errors.New("unexpected status-code: 204"),
Expand All @@ -748,7 +742,7 @@ func TestMakeDecision(t *testing.T) {
Name: "bad decoding",
OwnerID: "test-owner",
Request: DecisionRequest{},
Handler: func(w http.ResponseWriter, r *http.Request) {
Handler: func(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, "not a json response")
},
ExpectedError: errors.New("failed to decode response body: invalid character 'o' in literal null (expecting 'u')"),
Expand Down
Loading

0 comments on commit 571ae88

Please sign in to comment.