Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lx9FNLG2CqeBbVGGd9bFsP
103 lines
3.1 KiB
Go
103 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"github.com/v10networks/nmon/internal/auth/oidctest"
|
|
)
|
|
|
|
// devTB lets the test-only mock issuer run outside a test binary: it supplies the three methods oidctest uses
|
|
// (Helper, Fatal, Cleanup) and leaves the rest of testing.TB nil.
|
|
type devTB struct {
|
|
testing.TB
|
|
cleanups []func()
|
|
}
|
|
|
|
func (*devTB) Helper() {}
|
|
|
|
func (*devTB) Fatal(args ...any) { panic(fmt.Sprint(args...)) }
|
|
|
|
func (d *devTB) Cleanup(f func()) { d.cleanups = append(d.cleanups, f) }
|
|
|
|
func (d *devTB) close() {
|
|
for i := len(d.cleanups) - 1; i >= 0; i-- {
|
|
d.cleanups[i]()
|
|
}
|
|
}
|
|
|
|
// oidcMockCmd runs internal/auth/oidctest as a standalone OpenID Connect issuer for the M6 gate and for
|
|
// screenshots. The issuer approves every login at once as the "current user"; a control listener changes who
|
|
// that is: GET /user?sub=ID&name=NAME&groups=a,b on --control. The issuer URL is written to --issuer-file.
|
|
func oidcMockCmd(ctx context.Context, args []string) error {
|
|
fs := devFlagSet("oidc-mock")
|
|
control := fs.String("control", "[::1]:0", "listen address of the control endpoint")
|
|
clientID := fs.String("client-id", "nmon", "OIDC client id the issuer accepts")
|
|
clientSecret := fs.String("client-secret", "s3cret", "OIDC client secret the issuer accepts")
|
|
groupsClaim := fs.String("groups-claim", "groups", "name of the groups claim")
|
|
issuerFile := fs.String("issuer-file", "", "file that receives the issuer URL")
|
|
portFile := fs.String("port-file", "", "file that receives the control port")
|
|
if err := fs.Parse(args); err != nil {
|
|
return flagErr(err)
|
|
}
|
|
tb := &devTB{}
|
|
defer tb.close()
|
|
srv := oidctest.New(tb)
|
|
srv.ClientID, srv.ClientSecret, srv.GroupsClaim = *clientID, *clientSecret, *groupsClaim
|
|
ln, err := net.Listen("tcp", *control)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := writePort(*portFile, ln); err != nil {
|
|
return err
|
|
}
|
|
if *issuerFile != "" {
|
|
if err := os.WriteFile(*issuerFile, []byte(srv.Issuer()), 0o600); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /user", func(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
u := oidctest.User{Sub: q.Get("sub"), Name: q.Get("name"), Email: q.Get("email")}
|
|
if g := q.Get("groups"); g != "" {
|
|
u.Groups = strings.Split(g, ",")
|
|
}
|
|
if u.Sub == "" {
|
|
http.Error(w, "sub is required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
srv.SetUser(u)
|
|
w.WriteHeader(http.StatusNoContent)
|
|
})
|
|
hs := &http.Server{Handler: mux, ReadHeaderTimeout: 10 * time.Second}
|
|
go func() { <-ctx.Done(); _ = hs.Close() }()
|
|
fmt.Printf("oidc-mock issuer %s control %s\n", srv.Issuer(), ln.Addr())
|
|
if err := hs.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// bcryptCmd prints the bcrypt hash of its argument: the content of a break-glass password file.
|
|
func bcryptCmd(_ context.Context, args []string) error {
|
|
if len(args) != 1 {
|
|
return errors.New("usage: nmon-devtools bcrypt PASSWORD")
|
|
}
|
|
h, err := bcrypt.GenerateFromPassword([]byte(args[0]), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(string(h))
|
|
return nil
|
|
}
|