mirror of
https://dev.azure.com/schwarzit/schwarzit.stackit-public/_git/audit-go
synced 2026-02-18 13:42:01 +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 {
|
type RequestUrl struct {
|
||||||
Path string
|
Path string
|
||||||
RawQuery string
|
RawQuery *string
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuditRequest bundles request related parameters
|
// AuditRequest bundles request related parameters
|
||||||
|
|
@ -263,7 +263,7 @@ func NewAuditLogEntry(
|
||||||
// Get response body
|
// Get response body
|
||||||
responseBody, err := NewResponseBody(auditResponse.ResponseBodyBytes)
|
responseBody, err := NewResponseBody(auditResponse.ResponseBodyBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ErrInvalidResponse
|
return nil, errors.Join(err, ErrInvalidResponse)
|
||||||
}
|
}
|
||||||
var responseLength *int64 = nil
|
var responseLength *int64 = nil
|
||||||
if responseBody != nil {
|
if responseBody != nil {
|
||||||
|
|
@ -274,7 +274,7 @@ func NewAuditLogEntry(
|
||||||
// Get request body
|
// Get request body
|
||||||
requestBody, err := NewRequestBody(auditRequest.Request)
|
requestBody, err := NewRequestBody(auditRequest.Request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ErrInvalidRequestBody
|
return nil, errors.Join(err, ErrInvalidRequestBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get audit attributes from request
|
// Get audit attributes from request
|
||||||
|
|
@ -454,8 +454,8 @@ func NewRequestAttributes(
|
||||||
|
|
||||||
rawQuery := request.URL.RawQuery
|
rawQuery := request.URL.RawQuery
|
||||||
var query *string = nil
|
var query *string = nil
|
||||||
if rawQuery != "" {
|
if rawQuery != nil && *rawQuery != "" {
|
||||||
escapedQuery := url.QueryEscape(rawQuery)
|
escapedQuery := url.QueryEscape(*rawQuery)
|
||||||
query = &escapedQuery
|
query = &escapedQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -725,7 +725,7 @@ func parseClaimsFromAuthorizationHeader(authorizationHeader string) (map[string]
|
||||||
// base64 decoding
|
// base64 decoding
|
||||||
decodedString, err := base64.RawURLEncoding.DecodeString(authorizationHeaderParts[1])
|
decodedString, err := base64.RawURLEncoding.DecodeString(authorizationHeaderParts[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return parsedClaims, nil, ErrInvalidBearerToken
|
return parsedClaims, nil, errors.Join(err, ErrInvalidBearerToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
// unmarshall claim part of token
|
// unmarshall claim part of token
|
||||||
|
|
|
||||||
|
|
@ -154,9 +154,10 @@ func Test_NewRequestMetadata(t *testing.T) {
|
||||||
requestHeaders["User-Agent"] = []string{userAgent}
|
requestHeaders["User-Agent"] = []string{userAgent}
|
||||||
requestHeaders["Custom"] = []string{"customHeader"}
|
requestHeaders["Custom"] = []string{"customHeader"}
|
||||||
|
|
||||||
|
queryString := "topic=project"
|
||||||
request := Request{
|
request := Request{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
URL: RequestUrl{Path: "/audit/new", RawQuery: "topic=project"},
|
URL: RequestUrl{Path: "/audit/new", RawQuery: &queryString},
|
||||||
Host: "localhost:8080",
|
Host: "localhost:8080",
|
||||||
Proto: "HTTP/1.1",
|
Proto: "HTTP/1.1",
|
||||||
Scheme: "http",
|
Scheme: "http",
|
||||||
|
|
@ -220,7 +221,33 @@ func Test_NewRequestMetadata(t *testing.T) {
|
||||||
t.Run("without query parameters", func(t *testing.T) {
|
t.Run("without query parameters", func(t *testing.T) {
|
||||||
request := Request{
|
request := Request{
|
||||||
Method: "GET",
|
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",
|
Host: "localhost:8080",
|
||||||
Proto: "HTTP/1.1",
|
Proto: "HTTP/1.1",
|
||||||
Header: requestHeaders,
|
Header: requestHeaders,
|
||||||
|
|
@ -245,7 +272,7 @@ func Test_NewRequestMetadata(t *testing.T) {
|
||||||
t.Run("without request id", func(t *testing.T) {
|
t.Run("without request id", func(t *testing.T) {
|
||||||
request := Request{
|
request := Request{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
URL: RequestUrl{Path: "/audit/new", RawQuery: "topic=project"},
|
URL: RequestUrl{Path: "/audit/new", RawQuery: &queryString},
|
||||||
Host: "localhost:8080",
|
Host: "localhost:8080",
|
||||||
Proto: "HTTP/1.1",
|
Proto: "HTTP/1.1",
|
||||||
Header: requestHeaders,
|
Header: requestHeaders,
|
||||||
|
|
@ -269,7 +296,7 @@ func Test_NewRequestMetadata(t *testing.T) {
|
||||||
for _, httpMethod := range httpMethods {
|
for _, httpMethod := range httpMethods {
|
||||||
request := Request{
|
request := Request{
|
||||||
Method: httpMethod,
|
Method: httpMethod,
|
||||||
URL: RequestUrl{Path: "/audit/new", RawQuery: "topic=project"},
|
URL: RequestUrl{Path: "/audit/new", RawQuery: &queryString},
|
||||||
Host: "localhost:8080",
|
Host: "localhost:8080",
|
||||||
Proto: "HTTP/1.1",
|
Proto: "HTTP/1.1",
|
||||||
Header: requestHeaders,
|
Header: requestHeaders,
|
||||||
|
|
@ -293,7 +320,7 @@ func Test_NewRequestMetadata(t *testing.T) {
|
||||||
t.Run("unknown http method", func(t *testing.T) {
|
t.Run("unknown http method", func(t *testing.T) {
|
||||||
request := Request{
|
request := Request{
|
||||||
Method: "",
|
Method: "",
|
||||||
URL: RequestUrl{Path: "/audit/new", RawQuery: "topic=project"},
|
URL: RequestUrl{Path: "/audit/new", RawQuery: &queryString},
|
||||||
Host: "localhost:8080",
|
Host: "localhost:8080",
|
||||||
Proto: "HTTP/1.1",
|
Proto: "HTTP/1.1",
|
||||||
Header: requestHeaders,
|
Header: requestHeaders,
|
||||||
|
|
@ -678,9 +705,10 @@ func Test_NewAuditLogEntry(t *testing.T) {
|
||||||
requestBody := make(map[string]interface{})
|
requestBody := make(map[string]interface{})
|
||||||
requestBody["key"] = "request"
|
requestBody["key"] = "request"
|
||||||
requestBodyBytes, _ := json.Marshal(requestBody)
|
requestBodyBytes, _ := json.Marshal(requestBody)
|
||||||
|
query := "topic=project"
|
||||||
request := Request{
|
request := Request{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
URL: RequestUrl{Path: "/audit/new", RawQuery: "topic=project"},
|
URL: RequestUrl{Path: "/audit/new", RawQuery: &query},
|
||||||
Host: "localhost:8080",
|
Host: "localhost:8080",
|
||||||
Proto: "HTTP/1.1",
|
Proto: "HTTP/1.1",
|
||||||
Scheme: "http",
|
Scheme: "http",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue