Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use slog #7

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"os/exec"
Expand All @@ -22,17 +22,20 @@ func init() {

func main() {
if os.Getenv("JWKS_URI") == "" {
log.Fatal("JWKS_URI is required. e.g. JWKS_URI=https://gitlab.com/oauth/discovery/keys")
slog.Error("JWKS_URI is required. e.g. JWKS_URI=https://gitlab.com/oauth/discovery/keys")
os.Exit(1)
}
if os.Getenv("JWT_AUD") == "" {
log.Fatal("JWT_AUD is required. This needs to be the aud in the JWT you except this service to handle.")
slog.Error("JWT_AUD is required. This needs to be the aud in the JWT you except this service to handle.")
os.Exit(1)
}

http.HandleFunc("/", Rollout)
log.Println("Server is running on :8080")
slog.Info("Server is running on :8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("Unable to start service")
slog.Error("Unable to start service")
os.Exit(1)
}
}

Expand All @@ -41,7 +44,7 @@ func Rollout(w http.ResponseWriter, r *http.Request) {

a := r.Header.Get("Authorization")
if len(a) < 10 {
log.Println("Not auth header for", realIp, ",", lastIP)
slog.Info("Not auth header", "forwarded-ip", realIp, "lasthop-ip", lastIP)
http.Error(w, "need authorizaton: bearer xyz header", http.StatusUnauthorized)
return
}
Expand All @@ -51,13 +54,13 @@ func Rollout(w http.ResponseWriter, r *http.Request) {
// Parse and verify the token
token, err := jwt.Parse(tokenString, ParseToken)
if err != nil {
log.Println("Failed to verify token for", realIp, ",", lastIP, err.Error())
slog.Info("Failed to verify token for", "forwarded-ip", realIp, "lasthop-ip", lastIP, "err", err.Error())
http.Error(w, "Failed to verify token.", http.StatusUnauthorized)
return
}

if !token.Valid {
log.Println("Invalid token for", realIp, ",", lastIP)
slog.Info("Invalid token for", "forwarded-ip", realIp, "lasthop-ip", lastIP)
http.Error(w, "Invalid token", http.StatusUnauthorized)
return
}
Expand All @@ -68,19 +71,19 @@ func Rollout(w http.ResponseWriter, r *http.Request) {
var cc map[string]string
err = json.Unmarshal([]byte(ccStr), &cc)
if err != nil {
log.Println("Unable to read token claims for", realIp, ",", lastIP)
slog.Info("Unable to read token claims", "forwarded-ip", realIp, "lasthop-ip", lastIP)
http.Error(w, "Invalid token", http.StatusUnauthorized)
return
}
for k, v := range cc {
if claims[k] != v {
log.Println("Claim for", k, "doesn't match", realIp, ",", lastIP)
slog.Info("Claim doesn't match", "claim", k, "forwarded-ip", realIp, "lasthop-ip", lastIP)
http.Error(w, "Invalid token", http.StatusUnauthorized)
return
}
}
} else if !ok {
log.Println("Unable to read token claims for", realIp, ",", lastIP)
slog.Info("Unable to read token claims", "forwarded-ip", realIp, "lasthop-ip", lastIP)
http.Error(w, "Invalid token", http.StatusUnauthorized)
return
}
Expand All @@ -96,13 +99,12 @@ func Rollout(w http.ResponseWriter, r *http.Request) {
cmd.Stderr = &stdErr
cmd.Env = os.Environ()
if err := cmd.Run(); err != nil {
log.Printf("Error running %s command: %s", cmd.String(), stdOut.String())
log.Printf("stderr: %s", stdErr.String())
slog.Error("Error running", "command", cmd.String(), "stdout", stdOut.String(), "stderr", stdErr.String())
http.Error(w, "Script execution failed", http.StatusInternalServerError)
return
}

log.Println("Rollout complete for", realIp, ",", lastIP)
slog.Info("Rollout complete for", "forwarded-ip", realIp, "lasthop-ip", lastIP)
fmt.Fprintln(w, "Rollout complete")
}

Expand Down Expand Up @@ -130,7 +132,7 @@ func ParseToken(token *jwt.Token) (interface{}, error) {
jwksUri := os.Getenv("JWKS_URI")
jwksSet, err := jwk.Fetch(ctx, jwksUri)
if err != nil {
log.Fatalf("Unable to fetch JWK set from %s: %v", jwksUri, err)
return nil, fmt.Errorf("Unable to fetch JWK set from %s: %v", jwksUri, err)
}
// Find the appropriate key in JWKS
key, ok := jwksSet.LookupKeyID(kid)
Expand Down Expand Up @@ -171,7 +173,8 @@ func getArgs() []string {
}
rolloutArgs, err := shlex.Split(args)
if err != nil {
log.Fatalf("Error parsing ROLLOUT_ARGS %s: %v", args, err)
slog.Error("Error parsing ROLLOUT_ARGS", args, "err", err)
os.Exit(1)
}

return rolloutArgs
Expand Down
17 changes: 11 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"log/slog"
"math/big"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -73,12 +73,14 @@ func setupMockJwksServer(pub *rsa.PublicKey, kid string) *httptest.Server {
w.WriteHeader(http.StatusOK)
jwks, err := mockJWKS(pub, kid)
if err != nil {
log.Fatalf("Unable to generate RSA keys: %v", err)
slog.Error("Unable to generate RSA keys", "err", err)
os.Exit(1)
}

_, err = w.Write([]byte(jwks))
if err != nil {
log.Fatalf("Unable to generate RSA keys: %v", err)
slog.Error("Unable to generate RSA keys", "err", err)
os.Exit(1)
}
})

Expand All @@ -95,7 +97,8 @@ func createMockJwksServer() *httptest.Server {
claim = "bar"
privateKey, publicKey, err = GenerateRSAKeys()
if err != nil {
log.Fatalf("Unable to generate RSA keys: %v", err)
slog.Error("Unable to generate RSA keys", "err", err)
os.Exit(1)
}
testServer := setupMockJwksServer(publicKey, kid)
os.Setenv("JWKS_URI", fmt.Sprintf("%s/oauth/discovery/keys", testServer.URL))
Expand Down Expand Up @@ -144,7 +147,8 @@ func TestRollout(t *testing.T) {
// make sure the test file doesn't exist
err := RemoveFileIfExists(testFile)
if err != nil {
log.Fatalf("Unable to cleanup test file: %v", err)
slog.Error("Unable to cleanup test file", "err", err)
os.Exit(1)
}

s := createMockJwksServer()
Expand Down Expand Up @@ -298,7 +302,8 @@ func TestRollout(t *testing.T) {
// cleanup
err = RemoveFileIfExists(f)
if err != nil {
log.Fatalf("Unable to cleanup test file: %v", err)
slog.Error("Unable to cleanup test file", "file", f, "err", err)
os.Exit(1)
}
}
}
Expand Down
Loading