mirror of
https://dev.azure.com/schwarzit/schwarzit.stackit-public/_git/audit-go
synced 2026-02-08 00:57:24 +00:00
Allow nil as query string in convenience code
This commit is contained in:
parent
313486e30b
commit
f8d0efe6c3
2 changed files with 40 additions and 12 deletions
|
|
@ -39,7 +39,7 @@ type Request struct {
|
|||
|
||||
type RequestUrl struct {
|
||||
Path string
|
||||
RawQuery string
|
||||
RawQuery *string
|
||||
}
|
||||
|
||||
// AuditRequest bundles request related parameters
|
||||
|
|
@ -263,7 +263,7 @@ func NewAuditLogEntry(
|
|||
// Get response body
|
||||
responseBody, err := NewResponseBody(auditResponse.ResponseBodyBytes)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidResponse
|
||||
return nil, errors.Join(err, ErrInvalidResponse)
|
||||
}
|
||||
var responseLength *int64 = nil
|
||||
if responseBody != nil {
|
||||
|
|
@ -274,7 +274,7 @@ func NewAuditLogEntry(
|
|||
// Get request body
|
||||
requestBody, err := NewRequestBody(auditRequest.Request)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidRequestBody
|
||||
return nil, errors.Join(err, ErrInvalidRequestBody)
|
||||
}
|
||||
|
||||
// Get audit attributes from request
|
||||
|
|
@ -454,8 +454,8 @@ func NewRequestAttributes(
|
|||
|
||||
rawQuery := request.URL.RawQuery
|
||||
var query *string = nil
|
||||
if rawQuery != "" {
|
||||
escapedQuery := url.QueryEscape(rawQuery)
|
||||
if rawQuery != nil && *rawQuery != "" {
|
||||
escapedQuery := url.QueryEscape(*rawQuery)
|
||||
query = &escapedQuery
|
||||
}
|
||||
|
||||
|
|
@ -725,7 +725,7 @@ func parseClaimsFromAuthorizationHeader(authorizationHeader string) (map[string]
|
|||
// base64 decoding
|
||||
decodedString, err := base64.RawURLEncoding.DecodeString(authorizationHeaderParts[1])
|
||||
if err != nil {
|
||||
return parsedClaims, nil, ErrInvalidBearerToken
|
||||
return parsedClaims, nil, errors.Join(err, ErrInvalidBearerToken)
|
||||
}
|
||||
|
||||
// unmarshall claim part of token
|
||||
|
|
|
|||
|
|
@ -154,9 +154,10 @@ func Test_NewRequestMetadata(t *testing.T) {
|
|||
requestHeaders["User-Agent"] = []string{userAgent}
|
||||
requestHeaders["Custom"] = []string{"customHeader"}
|
||||
|
||||
queryString := "topic=project"
|
||||
request := Request{
|
||||
Method: "GET",
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: "topic=project"},
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: &queryString},
|
||||
Host: "localhost:8080",
|
||||
Proto: "HTTP/1.1",
|
||||
Scheme: "http",
|
||||
|
|
@ -220,7 +221,33 @@ func Test_NewRequestMetadata(t *testing.T) {
|
|||
t.Run("without query parameters", func(t *testing.T) {
|
||||
request := Request{
|
||||
Method: "GET",
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: ""},
|
||||
URL: RequestUrl{Path: "/audit/new"},
|
||||
Host: "localhost:8080",
|
||||
Proto: "HTTP/1.1",
|
||||
Header: requestHeaders,
|
||||
}
|
||||
|
||||
requestMetadata := NewRequestMetadata(
|
||||
&request,
|
||||
filteredHeaders,
|
||||
&requestId,
|
||||
requestScheme,
|
||||
requestTime,
|
||||
clientIp,
|
||||
authenticationPrincipal,
|
||||
audiences,
|
||||
auditClaims,
|
||||
)
|
||||
|
||||
verifyRequestMetadata(requestMetadata, &requestId)
|
||||
assert.Nil(t, requestMetadata.RequestAttributes.Query)
|
||||
})
|
||||
|
||||
t.Run("with empty query parameters", func(t *testing.T) {
|
||||
emptyQuery := ""
|
||||
request := Request{
|
||||
Method: "GET",
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: &emptyQuery},
|
||||
Host: "localhost:8080",
|
||||
Proto: "HTTP/1.1",
|
||||
Header: requestHeaders,
|
||||
|
|
@ -245,7 +272,7 @@ func Test_NewRequestMetadata(t *testing.T) {
|
|||
t.Run("without request id", func(t *testing.T) {
|
||||
request := Request{
|
||||
Method: "GET",
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: "topic=project"},
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: &queryString},
|
||||
Host: "localhost:8080",
|
||||
Proto: "HTTP/1.1",
|
||||
Header: requestHeaders,
|
||||
|
|
@ -269,7 +296,7 @@ func Test_NewRequestMetadata(t *testing.T) {
|
|||
for _, httpMethod := range httpMethods {
|
||||
request := Request{
|
||||
Method: httpMethod,
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: "topic=project"},
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: &queryString},
|
||||
Host: "localhost:8080",
|
||||
Proto: "HTTP/1.1",
|
||||
Header: requestHeaders,
|
||||
|
|
@ -293,7 +320,7 @@ func Test_NewRequestMetadata(t *testing.T) {
|
|||
t.Run("unknown http method", func(t *testing.T) {
|
||||
request := Request{
|
||||
Method: "",
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: "topic=project"},
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: &queryString},
|
||||
Host: "localhost:8080",
|
||||
Proto: "HTTP/1.1",
|
||||
Header: requestHeaders,
|
||||
|
|
@ -678,9 +705,10 @@ func Test_NewAuditLogEntry(t *testing.T) {
|
|||
requestBody := make(map[string]interface{})
|
||||
requestBody["key"] = "request"
|
||||
requestBodyBytes, _ := json.Marshal(requestBody)
|
||||
query := "topic=project"
|
||||
request := Request{
|
||||
Method: "GET",
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: "topic=project"},
|
||||
URL: RequestUrl{Path: "/audit/new", RawQuery: &query},
|
||||
Host: "localhost:8080",
|
||||
Proto: "HTTP/1.1",
|
||||
Scheme: "http",
|
||||
|
|
|
|||
Loading…
Reference in a new issue