package bslauth import ( "encoding/json" "flag" "fmt" "os" "testing" "edge-infra.dev/test/f2" "edge-infra.dev/test/f2/fctx" ) // Identifier is a consistent way of referring to users across multiple edge // environments. A user with a given Identifier will have the same roles across // multiple environments, even if the login details change. type Identifier string // All currently configured users const ( BannerAdmin Identifier = "banner-admin" // Identifies an user with the banner admin role ) type BSLAuth struct { APIEndpoint string users map[string]User } // Finds the User login details in the current env given an identifier func (b *BSLAuth) User(id Identifier) User { return b.users[string(id)] } // User holds the Edge Login details for an user in the given environment type User struct { Username string Password string Org string `json:"organization"` } func FromContext(ctx fctx.Context) (*BSLAuth, error) { v := fctx.ValueFrom[BSLAuth](ctx) if v == nil { return nil, fmt.Errorf("%w: bslAuth extension", fctx.ErrNotFound) } return v, nil } func FromContextT(ctx fctx.Context, t *testing.T) *BSLAuth { return fctx.ValueFromT[BSLAuth](ctx, t) } func New() *BSLAuth { bslAuth := &BSLAuth{} return bslAuth } func (b *BSLAuth) RegisterFns(f f2.Framework) { f.Setup(func(ctx f2.Context) (f2.Context, error) { creds, err := os.ReadFile("/mnt/bslcreds/bslcreds") if err != nil { return ctx, fmt.Errorf("Unable to read BSL credentials: %w", err) } err = json.Unmarshal(creds, &b.users) return ctx, err }) } // BindFlags registers test flags for the framework extension. func (b *BSLAuth) BindFlags(fs *flag.FlagSet) { fs.StringVar(&b.APIEndpoint, "api-endpoint", "", "edge api endpoint", ) } // IntoContext stores the framework extension in the test context. func (b *BSLAuth) IntoContext(ctx fctx.Context) fctx.Context { return fctx.ValueInto(ctx, b) }