Skip to content

Commit

Permalink
refactor: Use store v2 in Protobuf handlers
Browse files Browse the repository at this point in the history
Signed-off-by: jay-dee7 <me@jsdp.dev>
  • Loading branch information
jay-dee7 committed Oct 21, 2023
1 parent a1c2832 commit 81b4c83
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 56 deletions.
13 changes: 2 additions & 11 deletions cmd/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/containerish/OpenRegistry/registry/v2/extensions"
"github.com/containerish/OpenRegistry/router"
github_actions_server "github.com/containerish/OpenRegistry/services/kon/github_actions/v1/server"
"github.com/containerish/OpenRegistry/store/postgres"
store_v2 "github.com/containerish/OpenRegistry/store/v2"
"github.com/containerish/OpenRegistry/store/v2/automation"
"github.com/containerish/OpenRegistry/store/v2/emails"
Expand Down Expand Up @@ -77,14 +76,6 @@ func RunRegistryServer(ctx *cli.Context) {
logger := telemetry.ZLogger(fluentBitCollector, cfg.Environment)
e := echo.New()

pgStore, err := postgres.New(&cfg.StoreConfig)
if err != nil {
color.Red("ERR_PG_CONN: %s", err.Error())
return
}
defer pgStore.Close()
_ = pgStore

rawDB := store_v2.NewDB(cfg.StoreConfig, cfg.Environment)
registryStore := registry_store.NewStore(rawDB, logger)
usersStore := users.NewStore(rawDB, logger)
Expand Down Expand Up @@ -123,7 +114,7 @@ func RunRegistryServer(ctx *cli.Context) {
if cfg.Integrations.GetGithubConfig() != nil && cfg.Integrations.GetGithubConfig().Enabled {
ghApp, err := github.NewGithubApp(
cfg.Integrations.GetGithubConfig(),
pgStore,
usersStore,
logger,
cfg.WebAppConfig.AllowedEndpoints,
cfg.Environment,
Expand All @@ -140,7 +131,7 @@ func RunRegistryServer(ctx *cli.Context) {
&cfg.Registry.Auth,
logger,
buildAutomationStore,
pgStore,
usersStore,
)
go func() {
hostPort := fmt.Sprintf("%s:%d", ghConfig.Host, ghConfig.Port)
Expand Down
8 changes: 4 additions & 4 deletions services/kon/github_actions/v1/server/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewGitHubAppUsernameInterceptor(
return nil, err
}

user, err := ghStore.GetUserById(ctx, userID, false, nil)
user, err := ghStore.GetUserByID(ctx, userID)
if err != nil {
logEvent.Str("error", err.Error()).Send()
return nil, connect.NewError(connect.CodeFailedPrecondition, err)
Expand All @@ -57,7 +57,7 @@ func PopulateContextWithUserInterceptor(
logEvent.Err(err).Send()
return nil, err
}
user, err := ghStore.GetUserById(ctx, userID, false, nil)
user, err := ghStore.GetUserByID(ctx, userID)
if err != nil {
logEvent.Err(err).Send()
return nil, connect.NewError(connect.CodeFailedPrecondition, err)
Expand Down Expand Up @@ -97,7 +97,7 @@ func (i *githubAppStreamingInterceptor) WrapUnary(next connect.UnaryFunc) connec
return nil, connect.NewError(connect.CodeUnauthenticated, err)
}

user, err := i.store.GetUserById(ctx, userID, false, nil)
user, err := i.store.GetUserByID(ctx, userID)
if err != nil {
logEvent.Str("error", err.Error()).Send()
return nil, connect.NewError(connect.CodeFailedPrecondition, err)
Expand Down Expand Up @@ -158,7 +158,7 @@ func (i *githubAppStreamingInterceptor) WrapStreamingHandler(
logEvent.Err(err).Send()
return connect.NewError(connect.CodeUnauthenticated, err)
}
user, err := i.store.GetUserById(ctx, userID, false, nil)
user, err := i.store.GetUserByID(ctx, userID)
if err != nil {
logEvent.Str("error", err.Error()).Send()
return connect.NewError(connect.CodeFailedPrecondition, err)
Expand Down
82 changes: 50 additions & 32 deletions services/kon/github_actions/v1/server/interceptor_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,110 +10,124 @@ import (
"github.com/bufbuild/connect-go"
"github.com/containerish/OpenRegistry/auth"
"github.com/containerish/OpenRegistry/telemetry"
"github.com/fatih/color"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
)

func getTokenFromReq(req connect.AnyRequest, jwtSigningPubKey *rsa.PublicKey) (string, error) {
func getTokenFromReq(req connect.AnyRequest, jwtSigningPubKey *rsa.PublicKey) (uuid.UUID, error) {
token, err := tryTokenFromReqHeaders(req, jwtSigningPubKey)
if err != nil {
token, err = tryTokenFromReqCookies(req)
if err != nil {
return "", err
return uuid.Nil, fmt.Errorf("getTokenFromReq: tryTokenFromReqCookies: %w", err)
}
}

return token, nil
}

func tryTokenFromReqCookies(req connect.AnyRequest) (string, error) {
func tryTokenFromReqCookies(req connect.AnyRequest) (uuid.UUID, error) {
tmpReq := http.Request{Header: req.Header()}
sessionCookie, err := tmpReq.Cookie("session_id")
if err != nil {
return "", err
return uuid.Nil, fmt.Errorf("tryTokenFromReqCookies: ERR_NO_COOKIE: %w", err)
}

sessionID, err := url.QueryUnescape(sessionCookie.Value)
if err != nil {
return "", err
return uuid.Nil, fmt.Errorf("tryTokenFromReqCookies: ERR_WRONG_ENCODING: %w", err)
}

return sessionID, nil
parsedID, err := uuid.Parse(sessionID)
if err != nil {
return uuid.Nil, fmt.Errorf("tryTokenFromReqCookies: ERR_UUID_PARSE: %w", err)
}

return parsedID, nil
}

func tryTokenFromReqHeaders(req connect.AnyRequest, jwtSigningPubKey *rsa.PublicKey) (string, error) {
func tryTokenFromReqHeaders(req connect.AnyRequest, jwtSigningPubKey *rsa.PublicKey) (uuid.UUID, error) {
authToken := req.Header().Get("Authorization")
tokenParts := strings.Split(authToken, " ")
if len(tokenParts) == 2 {
if !strings.EqualFold(tokenParts[0], "Bearer") {
errMsg := fmt.Errorf("invalid authorization scheme")
return "", errMsg
errMsg := fmt.Errorf("tryTokenFromReqHeaders: invalid authorization scheme")
return uuid.Nil, errMsg
}

claims := &auth.Claims{}
token, err := jwt.ParseWithClaims(tokenParts[1], claims, func(t *jwt.Token) (interface{}, error) {
return jwtSigningPubKey, nil
})
if err != nil {
return "", err
return uuid.Nil, fmt.Errorf("tryTokenFromReqHeaders: ERR_JWT_CLAIM_PARSE: %w", err)
}

claims, ok := token.Claims.(*auth.Claims)
if !ok {
return "", fmt.Errorf("error parsing claims from token")
return uuid.Nil, fmt.Errorf("tryTokenFromReqHeaders: error parsing claims from token")
}

return claims.Subject, nil
parsedID, err := uuid.Parse(claims.Subject)
if err != nil {
return uuid.Nil, fmt.Errorf("tryTokenFromReqHeaders: ERR_UUID_PARSE: %w", err)
}
return parsedID, nil
}

errMsg := fmt.Errorf("auth token contains invalid parts")
return "", errMsg
return uuid.Nil, errMsg
}

func getTokenFromConn(
conn connect.StreamingHandlerConn,
jwtSigningPubKey *rsa.PublicKey,
logger telemetry.Logger,
) (string, error) {
) (uuid.UUID, error) {
token, err := tryTokenFromConnHeaders(conn, jwtSigningPubKey, logger)
if err != nil {
token, err = tryTokenFromConnCookies(conn)
if err != nil {
return "", err
return uuid.Nil, err
}
}

return token, nil
}

func tryTokenFromConnCookies(conn connect.StreamingHandlerConn) (string, error) {
func tryTokenFromConnCookies(conn connect.StreamingHandlerConn) (uuid.UUID, error) {
tmpReq := http.Request{Header: conn.RequestHeader()}
sessionCookie, err := tmpReq.Cookie("session_id")
if err != nil {
return "", err
return uuid.Nil, err
}

sessionID, err := url.QueryUnescape(sessionCookie.Value)
if err != nil {
return "", err
return uuid.Nil, err
}

return sessionID, nil
parsedID, err := uuid.Parse(sessionID)
if err != nil {
return uuid.Nil, fmt.Errorf("tryTokenFromConnCookies: ERR_UUID_PARSE: %w", err)
}

return parsedID, nil
}

func tryTokenFromConnHeaders(
conn connect.StreamingHandlerConn,
jwtSigningPubKey *rsa.PublicKey,
logger telemetry.Logger,
) (string, error) {
) (uuid.UUID, error) {
logEvent := logger.Debug().Str("procedure", conn.Spec().Procedure)
authToken := conn.RequestHeader().Get("Authorization")
tokenParts := strings.Split(authToken, " ")
if len(tokenParts) == 2 {
if !strings.EqualFold(tokenParts[0], "Bearer") {
errMsg := fmt.Errorf("invalid authorization scheme")
errMsg := fmt.Errorf("tryTokenFromConnHeaders: invalid authorization scheme")
logEvent.Err(errMsg).Send()
return "", errMsg
return uuid.Nil, errMsg
}

claims := &auth.Claims{}
Expand All @@ -122,27 +136,31 @@ func tryTokenFromConnHeaders(
})
if err != nil {
logEvent.Err(err).Send()
return "", err
return uuid.Nil, fmt.Errorf("tryTokenFromConnHeaders: ERR_JWT_CLAIM_PARSE: %w", err)
}

if !token.Valid {
errMsg := fmt.Errorf("JWT is invalid")
errMsg := fmt.Errorf("tryTokenFromConnHeaders: JWT is invalid")
logEvent.Err(errMsg).Send()
return "", errMsg
return uuid.Nil, errMsg
}

claims, ok := token.Claims.(*auth.Claims)
if !ok {
errMsg := fmt.Errorf("error parsing claims from token")
errMsg := fmt.Errorf("tryTokenFromConnHeaders: error parsing claims from token")
logEvent.Err(errMsg).Send()
return "", errMsg
return uuid.Nil, errMsg
}

parsedID, err := uuid.Parse(claims.Subject)
if err != nil {
return uuid.Nil, fmt.Errorf("tryTokenFromConnHeaders: ERR_UUID_PARSE: %w", err)
}

color.Yellow("claims from token: %#v", claims.Subject)
logEvent.Bool("success", true).Send()
return claims.Subject, nil
return parsedID, nil
}
errMsg := fmt.Errorf("invalid auth token")
errMsg := fmt.Errorf("tryTokenFromConnHeaders: invalid auth token")
logEvent.Err(errMsg).Send()
return "", errMsg
return uuid.Nil, errMsg
}
6 changes: 3 additions & 3 deletions services/kon/github_actions/v1/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/containerish/OpenRegistry/config"
github_actions_v1 "github.com/containerish/OpenRegistry/services/kon/github_actions/v1"
connect_v1 "github.com/containerish/OpenRegistry/services/kon/github_actions/v1/github_actions_v1connect"
build_automation_store "github.com/containerish/OpenRegistry/store/postgres/build_automation"
"github.com/containerish/OpenRegistry/store/v2/automation"
"github.com/containerish/OpenRegistry/telemetry"
"github.com/containerish/OpenRegistry/vcs"
"github.com/fatih/color"
Expand All @@ -22,7 +22,7 @@ type GitHubActionsServer struct {
config *config.Integration
github *github.Client
transport *ghinstallation.AppsTransport
store build_automation_store.BuildAutomationStore
store automation.BuildAutomationStore
activeLogStreamJobs map[string]*streamLogsJob
mu *sync.RWMutex
}
Expand All @@ -36,7 +36,7 @@ func NewGithubActionsServer(
config *config.Integration,
authConfig *config.Auth,
logger telemetry.Logger,
store build_automation_store.BuildAutomationStore,
store automation.BuildAutomationStore,
ghStore vcs.VCSStore,
) *http.ServeMux {
if !config.Enabled {
Expand Down
11 changes: 10 additions & 1 deletion vcs/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/containerish/OpenRegistry/telemetry"
"github.com/containerish/OpenRegistry/vcs"
"github.com/google/go-github/v50/github"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
)

Expand Down Expand Up @@ -106,7 +107,15 @@ func (gh *ghAppService) getUsernameMiddleware() echo.MiddlewareFunc {
return echoErr
}
userID := strings.Split(sessionID, ":")[1]
user, err := gh.store.GetUserById(c.Request().Context(), userID, false, nil)
parsedID, err := uuid.Parse(userID)
if err != nil {
echoErr := c.JSON(http.StatusForbidden, echo.Map{
"error": err.Error(),
})
gh.logger.Log(c, err).Send()
return echoErr
}
user, err := gh.store.GetUserByID(c.Request().Context(), parsedID)
if err != nil {
echoErr := c.JSON(http.StatusNotAcceptable, echo.Map{
"error": err.Error(),
Expand Down
4 changes: 2 additions & 2 deletions vcs/github/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (gh *ghAppService) HandleAppFinish(ctx echo.Context) error {
}
}

if err = gh.store.UpdateUser(ctx.Request().Context(), user); err != nil {
if _, err = gh.store.UpdateUser(ctx.Request().Context(), user); err != nil {
echoErr := ctx.JSON(http.StatusBadRequest, echo.Map{
"error": err.Error(),
})
Expand Down Expand Up @@ -107,7 +107,7 @@ func (gh *ghAppService) HandleSetupCallback(ctx echo.Context) error {
InstallationID: installationID,
}

if err := gh.store.UpdateUser(ctx.Request().Context(), user); err != nil {
if _, err = gh.store.UpdateUser(ctx.Request().Context(), user); err != nil {
// if err := gh.store.UpdateInstallationID(ctx.Request().Context(), installationID, username); err != nil {
echoErr := ctx.JSON(http.StatusBadRequest, echo.Map{
"error": err.Error(),
Expand Down
6 changes: 3 additions & 3 deletions vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/containerish/OpenRegistry/store/v2/types"
pgx "github.com/jackc/pgx/v4"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
)

Expand All @@ -23,8 +23,8 @@ type VCS interface {
}

type VCSStore interface {
GetUserById(ctx context.Context, userId string, wihtPassword bool, txn pgx.Tx) (*types.User, error)
UpdateUser(ctx context.Context, u *types.User) error
GetUserByID(ctx context.Context, userId uuid.UUID) (*types.User, error)
UpdateUser(ctx context.Context, u *types.User) (*types.User, error)
}

type Repository struct {
Expand Down

0 comments on commit 81b4c83

Please sign in to comment.