Skip to content

Commit

Permalink
Remove ping check since it is not compatible with Traefik analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebouthinon committed Jan 10, 2021
1 parent a963c7c commit b6a2719
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 155 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ middlewares:
kuzzle:
url: "http://localhost:7512" # required
routes: # optional
ping: /_publicApi
login: /_login/local
getCurrentUser: /_me # With Kuzzle v1 you must use '/users/_me'
allowedUsers: # optional
Expand All @@ -127,7 +126,6 @@ middlewares:
allowedUsers = ["admin", "developer"] # optional

[middlewares.your-well-named-middleware.plugin.traefik-kuzzle-auth.kuzzle.routes] # optional
ping = "/_publicApi"
login = "/_login/local"
getCurrentUser = "/_me" # With Kuzzle v1 you must use '/users/_me'

Expand All @@ -138,7 +136,6 @@ middlewares:
labels:
- "traefik.http.middlewares.your-well-named-middleware.plugin.traefik-kuzzle-auth.customRealm=Use a valid Kuzzle user to authenticate" # optional
- "traefik.http.middlewares.your-well-named-middleware.plugin.traefik-kuzzle-auth.kuzzle.url=http://kuzzle:7512" # required
- "traefik.http.middlewares.your-well-named-middleware.plugin.traefik-kuzzle-auth.kuzzle.routes.ping=/_publicApi" # optional
- "traefik.http.middlewares.your-well-named-middleware.plugin.traefik-kuzzle-auth.kuzzle.routes.login=/_login/local" # optional
- "traefik.http.middlewares.your-well-named-middleware.plugin.traefik-kuzzle-auth.kuzzle.routes.getCurrentUser=/_me" # With Kuzzle v1 you must use '/users/_me' (optional)
- "traefik.http.middlewares.kuzzle-auth.plugin.traefik-kuzzle-auth.kuzzle.allowedUsers=admin,developer" # optional
Expand Down
4 changes: 0 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ func (c *Config) addMissingFields() *Config {
c.CustomRealm = "Use a valid Kuzzle user to authenticate"
}

if c.Kuzzle.Routes.Ping == "" {
c.Kuzzle.Routes.Ping = "/_publicApi"
}

if c.Kuzzle.Routes.Login == "" {
c.Kuzzle.Routes.Login = "/_login/local"
}
Expand Down
3 changes: 0 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func TestConfig_addMissingFields(t *testing.T) {
Kuzzle: Kuzzle{
URL: "http://kuzzle:7512",
Routes: Routes{
Ping: "/_publicApi",
Login: "/_login/local",
GetCurrentUser: "/_me",
},
Expand All @@ -33,7 +32,6 @@ func TestConfig_addMissingFields(t *testing.T) {
Kuzzle: Kuzzle{
URL: "http://kuzzle:7512",
Routes: Routes{
Ping: "/_publicApi",
Login: "/_login/local",
GetCurrentUser: "/_me",
},
Expand All @@ -54,7 +52,6 @@ func TestConfig_addMissingFields(t *testing.T) {
Kuzzle: Kuzzle{
URL: "http://kuzzle:7512",
Routes: Routes{
Ping: "/_publicApi",
Login: "/_login/local",
GetCurrentUser: "/_me",
},
Expand Down
16 changes: 0 additions & 16 deletions kuzzle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (

// Routes used to request Kuzzle, can be customized
type Routes struct {
// Ping route used to test configured Kuzzle server reachability.
// The specified route must return 200 HTTP status code when called by anonymous user.
// Default is '/_publicApi' (see: https://docs.kuzzle.io/core/2/api/controllers/server/public-api/).
// You would like to modify this route if you performed security adjustement on your Kuzzle Server
Ping string `yaml:"ping,omitempty"`
// Login route used to log in to Kuzzle using Auth Basic user/pass.
// The specified route must return 200 HTTP status code and a valid JWT when called by anonymous user.
// Default is '/_login/local' (see: https://docs.kuzzle.io/core/2/api/controllers/auth/login/)
Expand Down Expand Up @@ -49,17 +44,6 @@ type Kuzzle struct {
JWT string
}

func (k *Kuzzle) ping() error {
url := fmt.Sprintf("%s%s", k.URL, k.Routes.Ping)
_, err := http.Get(url)

if err != nil {
return fmt.Errorf("Ping request send to %s failed: %v", url, err)
}

return nil
}

func (k *Kuzzle) login(user string, password string) error {
reqBody, _ := json.Marshal(map[string]string{
"username": user,
Expand Down
95 changes: 0 additions & 95 deletions kuzzle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,101 +7,6 @@ import (
"gopkg.in/h2non/gock.v1"
)

func TestKuzzle_ping(t *testing.T) {
type fields struct {
URL string
Routes Routes
AllowedUsers []string
JWT string
}
tests := []struct {
name string
fields fields
wantErr bool
mock Mock
}{
{
name: "Success",
fields: fields{
URL: "http://kuzzle:7512",
Routes: Routes{
Ping: "/_publicApi",
},
},
wantErr: false,
mock: Mock{
enabled: true,
statusCode: 200,
url: "http://kuzzle:7512",
route: "/_publicApi",
},
},
{
name: "Unreachable Kuzzle server",
fields: fields{
URL: "http://kuzzle:7512",
Routes: Routes{
Ping: "/_publicApi",
},
},
wantErr: true,
},
{
name: "Bad URL format",
fields: fields{
URL: "Bad",
Routes: Routes{
Ping: "/_publicApi",
},
},
wantErr: true,
mock: Mock{
enabled: true,
statusCode: 200,
url: "Bad",
route: "/_publicApi",
},
},
{
name: "Anonymous Unauthorized route",
fields: fields{
URL: "http://kuzzle:7512",
Routes: Routes{
Ping: "/_publicApi",
},
},
wantErr: true,
mock: Mock{
enabled: true,
statusCode: 401,
url: "http://localhost:7512",
route: "/_publicApi",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.mock.enabled {
defer gock.Off()
gock.
New(tt.mock.url).
Get(tt.mock.route).
Reply(tt.mock.statusCode)
}

k := &Kuzzle{
URL: tt.fields.URL,
Routes: tt.fields.Routes,
AllowedUsers: tt.fields.AllowedUsers,
JWT: tt.fields.JWT,
}
if err := k.ping(); (err != nil) != tt.wantErr {
t.Errorf("Kuzzle.ping() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestKuzzle_login(t *testing.T) {
type fields struct {
URL string
Expand Down
5 changes: 0 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package traefik_kuzzle_auth

import (
"context"
"fmt"
"net/http"
)

Expand All @@ -15,10 +14,6 @@ type KuzzleAuth struct {

// New created a new KuzzleBasicAuth plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if err := config.Kuzzle.ping(); err != nil {
return nil, fmt.Errorf("Unable to reach Kuzzle server at %s: %v", config.Kuzzle.URL, err)
}

return &KuzzleAuth{
next: next,
name: name,
Expand Down
33 changes: 4 additions & 29 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,20 @@ func TestNew(t *testing.T) {
args: args{
config: &Config{
Kuzzle: Kuzzle{
URL: "http://kuzzle:7512",
Routes: Routes{
Ping: "/_publicApi",
},
URL: "http://kuzzle:7512",
Routes: Routes{},
},
},
},
want: &KuzzleAuth{
config: &Config{
Kuzzle: Kuzzle{
URL: "http://kuzzle:7512",
Routes: Routes{
Ping: "/_publicApi",
},
URL: "http://kuzzle:7512",
Routes: Routes{},
},
},
},
wantErr: false,
mock: Mock{
enabled: true,
statusCode: 200,
url: "http://kuzzle:7512",
route: "/_publicApi",
},
},
{
name: "Kuzzle server ping failure",
args: args{
config: &Config{
Kuzzle: Kuzzle{
URL: "http://kuzzle:7512",
Routes: Routes{
Ping: "/_publicApi",
},
},
},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit b6a2719

Please sign in to comment.