-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Frank Jogeleit <frank.jogeleit@web.de>
- Loading branch information
Showing
11 changed files
with
267 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# Changelog | ||
|
||
## 1.6.0 | ||
|
||
* Support BasicAuth for REST APIs and metrics | ||
|
||
## 1.5.1 | ||
|
||
* Add zap.Logger | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package api | ||
|
||
import ( | ||
"crypto/sha256" | ||
"crypto/subtle" | ||
"net/http" | ||
) | ||
|
||
type BasicAuth struct { | ||
Username string | ||
Password string | ||
} | ||
|
||
func HTTPBasic(auth *BasicAuth, next http.HandlerFunc) http.HandlerFunc { | ||
if auth == nil { | ||
return next | ||
} | ||
|
||
expectedUsernameHash := sha256.Sum256([]byte(auth.Username)) | ||
expectedPasswordHash := sha256.Sum256([]byte(auth.Password)) | ||
|
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
username, password, ok := r.BasicAuth() | ||
if ok { | ||
|
||
usernameHash := sha256.Sum256([]byte(username)) | ||
passwordHash := sha256.Sum256([]byte(password)) | ||
|
||
usernameMatch := (subtle.ConstantTimeCompare(usernameHash[:], expectedUsernameHash[:]) == 1) | ||
passwordMatch := (subtle.ConstantTimeCompare(passwordHash[:], expectedPasswordHash[:]) == 1) | ||
|
||
if usernameMatch && passwordMatch { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
} | ||
|
||
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`) | ||
http.Error(w, "Unauthorized", http.StatusUnauthorized) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package secrets | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
v1 "k8s.io/client-go/kubernetes/typed/core/v1" | ||
"k8s.io/client-go/util/retry" | ||
) | ||
|
||
type Values struct { | ||
Username string `json:"username" mapstructure:"username"` | ||
Password string `json:"password" mapstructure:"password"` | ||
} | ||
|
||
type Client interface { | ||
Get(context.Context, string) (Values, error) | ||
} | ||
|
||
type k8sClient struct { | ||
client v1.SecretInterface | ||
} | ||
|
||
func (c *k8sClient) Get(ctx context.Context, name string) (Values, error) { | ||
var secret *corev1.Secret | ||
|
||
err := retry.OnError(retry.DefaultRetry, func(err error) bool { | ||
if _, ok := err.(errors.APIStatus); !ok { | ||
return true | ||
} | ||
|
||
if ok := errors.IsTimeout(err); ok { | ||
return true | ||
} | ||
|
||
if ok := errors.IsServerTimeout(err); ok { | ||
return true | ||
} | ||
|
||
if ok := errors.IsServiceUnavailable(err); ok { | ||
return true | ||
} | ||
|
||
return false | ||
}, func() error { | ||
var err error | ||
secret, err = c.client.Get(ctx, name, metav1.GetOptions{}) | ||
|
||
return err | ||
}) | ||
|
||
values := Values{} | ||
if err != nil { | ||
return values, err | ||
} | ||
|
||
if username, ok := secret.Data["username"]; ok { | ||
values.Username = string(username) | ||
} | ||
|
||
if password, ok := secret.Data["password"]; ok { | ||
values.Password = string(password) | ||
} | ||
|
||
return values, nil | ||
} | ||
|
||
func NewClient(secretClient v1.SecretInterface) Client { | ||
return &k8sClient{secretClient} | ||
} |
Oops, something went wrong.