Skip to content

Commit

Permalink
Merge pull request #3 from dgduncan/dev
Browse files Browse the repository at this point in the history
Merge dev to main
  • Loading branch information
dgduncan authored Feb 21, 2023
2 parents 1051160 + f162344 commit d1ba87f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ Implementations of certain hooks are inspired by other open source projects

### Hooks

#### Auth

##### HTTP

The HTTP hook is a simple HTTP hook that uses two hooks to authorize the client to connect to the broker and authorizes topic level ACLs.
It works by checking the response code of each endpoint. If an endpoint returns back a non `200` response a `false` is returned to the mochi hook


17 changes: 15 additions & 2 deletions http_auth_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
Expand Down Expand Up @@ -57,15 +58,20 @@ func (h *HTTPAuthHook) Provides(b byte) bool {

func (h *HTTPAuthHook) Init(config any) error {
if config == nil {
h.Log.Debug().Msg("nil config")
return nil
return errors.New("nil config")
}

authHookConfig, ok := config.(HTTPAuthHookConfig)
if !ok {
return errors.New("improper config")
}

fmt.Println(validateConfig(authHookConfig))

if !validateConfig(authHookConfig) {
return errors.New("hostname configs failed validation")
}

h.httpclient = NewTransport(authHookConfig.RoundTripper)

h.aclhost = authHookConfig.ACLHost
Expand Down Expand Up @@ -134,3 +140,10 @@ func (h *HTTPAuthHook) makeRequest(requestType, url string, payload any) (*http.

return resp, nil
}

func validateConfig(config HTTPAuthHookConfig) bool {
if config.ACLHost == "" || config.ClientAuthenticationHost == "" {
return false
}
return true
}
33 changes: 25 additions & 8 deletions http_auth_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,20 @@ func TestInit(t *testing.T) {
expectError: false,
},
{
name: "Success - nil config",
name: "Failure - nil config",
config: nil,
expectError: false,
expectError: true,
},
{
name: "Failure - improper config",
config: "",
expectError: true,
},
{
name: "Failure - hostname validation fail",
config: HTTPAuthHookConfig{},
expectError: true,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -106,7 +111,9 @@ func TestOnACLCheck(t *testing.T) {
{
name: "Success - Proper config",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: true,
mocks: func(ctx context.Context) {
Expand All @@ -119,7 +126,9 @@ func TestOnACLCheck(t *testing.T) {
{
name: "Error - HTTP error",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: false,
mocks: func(ctx context.Context) {
Expand All @@ -129,7 +138,9 @@ func TestOnACLCheck(t *testing.T) {
{
name: "Error - Non 2xx",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: false,
mocks: func(ctx context.Context) {
Expand Down Expand Up @@ -170,7 +181,9 @@ func TestOnConnectAuthenticate(t *testing.T) {
{
name: "Success - Proper config",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: true,
mocks: func(ctx context.Context) {
Expand All @@ -183,7 +196,9 @@ func TestOnConnectAuthenticate(t *testing.T) {
{
name: "Error - HTTP error",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: false,
mocks: func(ctx context.Context) {
Expand All @@ -193,7 +208,9 @@ func TestOnConnectAuthenticate(t *testing.T) {
{
name: "Error - Non 2xx",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: false,
mocks: func(ctx context.Context) {
Expand Down

0 comments on commit d1ba87f

Please sign in to comment.