From a6eba59bf5ed976f10925729c3ea9e7793d62b14 Mon Sep 17 00:00:00 2001 From: Mehul-Kumar-27 Date: Tue, 17 Dec 2024 20:30:18 +0530 Subject: [PATCH] bug fixes in robot account in ground control and fixing unauthorized error while pulling the images --- cmd/root.go | 9 +++++ config.json | 2 +- .../internal/database/groups.sql.go | 28 ++++++++++++++ ground-control/internal/server/handlers.go | 37 +++++++++++++++++-- ground-control/sql/queries/groups.sql | 4 ++ internal/state/state_process.go | 1 + internal/utils/utils.go | 7 ---- 7 files changed, 77 insertions(+), 11 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 814ca684..9c939c0e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,6 +2,7 @@ package cmd import ( "context" + "fmt" runtime "container-registry.com/harbor-satellite/cmd/container_runtime" "container-registry.com/harbor-satellite/internal/config" @@ -10,6 +11,7 @@ import ( "container-registry.com/harbor-satellite/internal/server" "container-registry.com/harbor-satellite/internal/utils" "container-registry.com/harbor-satellite/logger" + "container-registry.com/harbor-satellite/registry" "github.com/rs/zerolog" "github.com/spf13/cobra" "golang.org/x/sync/errgroup" @@ -95,6 +97,13 @@ func handleRegistrySetup(g *errgroup.Group, log *zerolog.Logger, cancel context. } } else { log.Info().Msg("Launching default registry") + var defaultZotConfig registry.DefaultZotConfig + err := registry.ReadConfig(config.GetZotConfigPath(), &defaultZotConfig) + if err != nil { + return fmt.Errorf("error reading config: %w", err) + } + defaultZotURL := defaultZotConfig.GetLocalRegistryURL() + config.SetRemoteRegistryURL(defaultZotURL) g.Go(func() error { if err := utils.LaunchDefaultZotRegistry(); err != nil { log.Error().Err(err).Msg("Error launching default registry") diff --git a/config.json b/config.json index f115b64b..9a926b45 100644 --- a/config.json +++ b/config.json @@ -10,7 +10,7 @@ ] }, "environment_variables": { - "ground_control_url": "", + "ground_control_url": "http://127.0.0.1:8080", "log_level": "info", "use_unsecure": true, "zot_config_path": "./registry/config.json", diff --git a/ground-control/internal/database/groups.sql.go b/ground-control/internal/database/groups.sql.go index bca81646..a931ca2c 100644 --- a/ground-control/internal/database/groups.sql.go +++ b/ground-control/internal/database/groups.sql.go @@ -90,6 +90,34 @@ func (q *Queries) GetGroupByName(ctx context.Context, groupName string) (Group, return i, err } +const getProjectsOfGroup = `-- name: GetProjectsOfGroup :many +SELECT projects FROM groups +WHERE group_name = $1 +` + +func (q *Queries) GetProjectsOfGroup(ctx context.Context, groupName string) ([][]string, error) { + rows, err := q.db.QueryContext(ctx, getProjectsOfGroup, groupName) + if err != nil { + return nil, err + } + defer rows.Close() + var items [][]string + for rows.Next() { + var projects []string + if err := rows.Scan(pq.Array(&projects)); err != nil { + return nil, err + } + items = append(items, projects) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const listGroups = `-- name: ListGroups :many SELECT id, group_name, registry_url, projects, created_at, updated_at FROM groups ` diff --git a/ground-control/internal/server/handlers.go b/ground-control/internal/server/handlers.go index 43324ca2..1ba8ad64 100644 --- a/ground-control/internal/server/handlers.go +++ b/ground-control/internal/server/handlers.go @@ -162,9 +162,9 @@ func (s *Server) registerSatelliteHandler(w http.ResponseWriter, r *http.Request } if len(req.Name) < 1 { - log.Println("name should be atleast one character long.") + log.Println("name should be at least one character long.") err := &AppError{ - Message: fmt.Sprintf("Error: name should be atleast one character long."), + Message: "Error: name should be at least one character long.", Code: http.StatusBadRequest, } HandleAppError(w, err) @@ -184,7 +184,7 @@ func (s *Server) registerSatelliteHandler(w http.ResponseWriter, r *http.Request if roboPresent { err := &AppError{ - Message: fmt.Sprintf("Error: Robot Account name already present. Try with different name"), + Message: "Error: Robot Account name already present. Try with different name", Code: http.StatusBadRequest, } HandleAppError(w, err) @@ -324,6 +324,37 @@ func (s *Server) registerSatelliteHandler(w http.ResponseWriter, r *http.Request return } + // Give permission to the robot account for the projects present in the group list + // fetch all the projects + for i := range *req.Groups { + projects, err := q.GetProjectsOfGroup(r.Context(), (*req.Groups)[i]) + if err != nil { + log.Println(err) + err := &AppError{ + Message: fmt.Sprintf("Error: fetching projects of group %v", err.Error()), + Code: http.StatusInternalServerError, + } + HandleAppError(w, err) + tx.Rollback() + return + } + project := projects[0] + + // give permission to the robot account for all the projects present in this group + _, err = utils.UpdateRobotProjects(r.Context(), project, strconv.FormatInt(rbt.ID, 10)) + if err != nil { + log.Println(err) + err := &AppError{ + Message: fmt.Sprintf("Error: updating robot account %v", err.Error()), + Code: http.StatusInternalServerError, + } + HandleAppError(w, err) + tx.Rollback() + return + } + + } + // Add token to DB token, err := GenerateRandomToken(32) if err != nil { diff --git a/ground-control/sql/queries/groups.sql b/ground-control/sql/queries/groups.sql index 62135303..49767d7c 100644 --- a/ground-control/sql/queries/groups.sql +++ b/ground-control/sql/queries/groups.sql @@ -22,3 +22,7 @@ WHERE group_name = $1; -- name: DeleteGroup :exec DELETE FROM groups WHERE id = $1; + +-- name: GetProjectsOfGroup :many +SELECT projects FROM groups +WHERE group_name = $1; diff --git a/internal/state/state_process.go b/internal/state/state_process.go index 68c6bd67..499253b1 100644 --- a/internal/state/state_process.go +++ b/internal/state/state_process.go @@ -329,6 +329,7 @@ func (f *FetchAndReplicateStateProcess) UpdateFetchProcessConfigFromZtr(username f.authConfig.SourceRegistryUserName = username f.authConfig.SourceRegistryPassword = password f.authConfig.SourceRegistry = utils.FormatRegistryURL(sourceRegistryURL) + f.Replicator = NewBasicReplicator(f.authConfig.SourceRegistryUserName, f.authConfig.SourceRegistryPassword, f.authConfig.SourceRegistry, f.authConfig.RemoteRegistryURL, f.authConfig.RemoteRegistryUserName, f.authConfig.RemoteRegistryPassword, f.authConfig.UseUnsecure) // The states contain all the states that this satellite needs to track thus we would have to add the new states to the state map // also we would have to remove the states that are not in the new states diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 04395721..b78e7b3a 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -50,13 +50,6 @@ func HandleOwnRegistry() error { // LaunchDefaultZotRegistry launches the default Zot registry using the Zot config path func LaunchDefaultZotRegistry() error { - var defaultZotConfig registry.DefaultZotConfig - err := registry.ReadConfig(config.GetZotConfigPath(), &defaultZotConfig) - if err != nil { - return fmt.Errorf("error reading config: %w", err) - } - defaultZotURL := defaultZotConfig.GetLocalRegistryURL() - config.SetRemoteRegistryURL(defaultZotURL) launch, err := registry.LaunchRegistry(config.GetZotConfigPath()) if !launch { return fmt.Errorf("error launching registry: %w", err)