Skip to content

Commit

Permalink
fix: [AH-306c]: fix anonymous flow (harness#2573)
Browse files Browse the repository at this point in the history
* lint fixes
* fix: [AH-306]: fix anonymous flow - address review comments
* fix: [AH-306c]: fix anonymous flow - address review comments
* fix: [AH-306c]: fix anonymous flow
* fix: [AH-306c]: fix anonymous flow
* fix: [AH-306c]: fix anonymous flow
  • Loading branch information
shivagowda authored and Harness committed Aug 24, 2024
1 parent af6e94b commit d52a142
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 45 deletions.
10 changes: 7 additions & 3 deletions registry/app/api/controller/metadata/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,13 @@ func CreateUpstreamProxyResponseJSONResponse(upstreamproxy *types.UpstreamProxy)
if api.AuthType(upstreamproxy.RepoAuthType) == api.AuthTypeUserPassword {
auth := api.UserPassword{}
auth.UserName = upstreamproxy.UserName
// FIXME: Mask this password.
auth.SecretIdentifier = &upstreamproxy.SecretIdentifier
auth.SecretSpaceId = &upstreamproxy.SecretSpaceID
auth.SecretIdentifier = &upstreamproxy.SecretIdentifier.String
auth.SecretSpaceId = nil
if upstreamproxy.SecretSpaceID.Valid {
// Convert int32 to int and assign to the expected field
secretSpaceID := int(upstreamproxy.SecretSpaceID.Int32)
auth.SecretSpaceId = &secretSpaceID
}
_ = configAuth.FromUserPassword(auth)
}

Expand Down
11 changes: 6 additions & 5 deletions registry/app/api/controller/metadata/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ func ValidateIdentifier(identifier string) error {
}

func ValidateUpstream(config *api.RegistryConfig) error {
if !commons.IsEmpty(config.Type) && config.Type == api.RegistryTypeUPSTREAM {
upstreamConfig, err := config.AsUpstreamConfig()
if err != nil {
return err
}
upstreamConfig, err := config.AsUpstreamConfig()
if err != nil {
return err
}
if !commons.IsEmpty(config.Type) && config.Type == api.RegistryTypeUPSTREAM &&
*upstreamConfig.Source != api.UpstreamConfigSourceDockerhub {
if commons.IsEmpty(upstreamConfig.Url) {
return errors.New("URL is required for upstream repository")
}
Expand Down
11 changes: 10 additions & 1 deletion registry/app/remote/adapter/native/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ func getPwd(
) string {
password := ""
if api.AuthType(reg.RepoAuthType) == api.AuthTypeUserPassword {
secret, err := secretStore.FindByIdentifier(ctx, int64(reg.SecretSpaceID), reg.SecretIdentifier)
secretSpaceID := int64(0)
if reg.SecretSpaceID.Valid {
secretSpaceID = int64(reg.SecretSpaceID.Int32)
}

secretIdentifier := ""
if reg.SecretIdentifier.Valid {
secretIdentifier = reg.SecretIdentifier.String
}
secret, err := secretStore.FindByIdentifier(ctx, secretSpaceID, secretIdentifier)
if err != nil {
log.Error().Msgf("failed to find secret: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion registry/app/store/database/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (a ArtifactDao) mapToInternalArtifact(ctx context.Context, in *types.Artifa
ID: in.ID,
Name: in.Name,
RegistryID: in.RegistryID,
Labels: getEmptySQLString(util.ArrToString(in.Labels)),
Labels: util.GetEmptySQLString(util.ArrToString(in.Labels)),
Enabled: in.Enabled,
CreatedAt: in.CreatedAt.UnixMilli(),
UpdatedAt: in.UpdatedAt.UnixMilli(),
Expand Down
17 changes: 5 additions & 12 deletions registry/app/store/database/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,27 +376,20 @@ func mapToInternalRegistry(ctx context.Context, in *types.Registry) *registryDB
Name: in.Name,
ParentID: in.ParentID,
RootParentID: in.RootParentID,
Description: getEmptySQLString(in.Description),
Description: util.GetEmptySQLString(in.Description),
Type: in.Type,
PackageType: in.PackageType,
UpstreamProxies: getEmptySQLString(util.Int64ArrToString(in.UpstreamProxies)),
AllowedPattern: getEmptySQLString(util.ArrToString(in.AllowedPattern)),
BlockedPattern: getEmptySQLString(util.ArrToString(in.BlockedPattern)),
Labels: getEmptySQLString(util.ArrToString(in.Labels)),
UpstreamProxies: util.GetEmptySQLString(util.Int64ArrToString(in.UpstreamProxies)),
AllowedPattern: util.GetEmptySQLString(util.ArrToString(in.AllowedPattern)),
BlockedPattern: util.GetEmptySQLString(util.ArrToString(in.BlockedPattern)),
Labels: util.GetEmptySQLString(util.ArrToString(in.Labels)),
CreatedAt: in.CreatedAt.UnixMilli(),
UpdatedAt: in.UpdatedAt.UnixMilli(),
CreatedBy: in.CreatedBy,
UpdatedBy: in.UpdatedBy,
}
}

func getEmptySQLString(str string) sql.NullString {
if commons.IsEmpty(str) {
return sql.NullString{String: str, Valid: false}
}
return sql.NullString{String: str, Valid: true}
}

func (r registryDao) Delete(ctx context.Context, parentID int64, name string) (err error) {
stmt := databaseg.Builder.Delete("registries").
Where("registry_parent_id = ? AND registry_name = ?", parentID, name)
Expand Down
8 changes: 4 additions & 4 deletions registry/app/store/database/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func (t tagDao) GetAllArtifactsByParentID(

if len(labels) > 0 {
sort.Strings(labels)
labelsVal := getEmptySQLString(util.ArrToString(labels))
labelsVal := util.GetEmptySQLString(util.ArrToString(labels))

labelsVal.String = labelSeparatorStart + labelsVal.String + labelSeparatorEnd
q = q.Where("'^_' || ar.artifact_labels || '^_' LIKE ?", labelsVal)
Expand Down Expand Up @@ -430,7 +430,7 @@ func (t tagDao) CountAllArtifactsByParentID(

if len(labels) > 0 {
sort.Strings(labels)
labelsVal := getEmptySQLString(util.ArrToString(labels))
labelsVal := util.GetEmptySQLString(util.ArrToString(labels))
labelsVal.String = labelSeparatorStart + labelsVal.String + labelSeparatorEnd
q = q.Where("'^_' || ar.artifact_labels || '^_' LIKE ?", labelsVal)
}
Expand Down Expand Up @@ -645,7 +645,7 @@ func (t tagDao) GetAllArtifactsByRepo(

if len(labels) > 0 {
sort.Strings(labels)
labelsVal := getEmptySQLString(util.ArrToString(labels))
labelsVal := util.GetEmptySQLString(util.ArrToString(labels))
labelsVal.String = labelSeparatorStart + labelsVal.String + labelSeparatorEnd
q = q.Where("'^_' || ar.artifact_labels || '^_' LIKE ?", labelsVal)
}
Expand Down Expand Up @@ -691,7 +691,7 @@ func (t tagDao) CountAllArtifactsByRepo(

if len(labels) > 0 {
sort.Strings(labels)
labelsVal := getEmptySQLString(util.ArrToString(labels))
labelsVal := util.GetEmptySQLString(util.ArrToString(labels))
labelsVal.String = labelSeparatorStart + labelsVal.String + labelSeparatorEnd
q = q.Where("'^_' || ar.artifact_labels || '^_' LIKE ?", labelsVal)
}
Expand Down
34 changes: 17 additions & 17 deletions registry/app/store/database/upstream_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ func NewUpstreamproxyDao(db *sqlx.DB, registryDao store.RegistryRepository) stor

// upstreamProxyConfigDB holds the record of an upstream_proxy_config in DB.
type upstreamProxyConfigDB struct {
ID int64 `db:"upstream_proxy_config_id"`
RegistryID int64 `db:"upstream_proxy_config_registry_id"`
Source string `db:"upstream_proxy_config_source"`
URL string `db:"upstream_proxy_config_url"`
AuthType string `db:"upstream_proxy_config_auth_type"`
UserName string `db:"upstream_proxy_config_user_name"`
SecretIdentifier string `db:"upstream_proxy_config_secret_identifier"`
SecretSpaceID int `db:"upstream_proxy_config_secret_space_id"`
Token string `db:"upstream_proxy_config_token"`
CreatedAt int64 `db:"upstream_proxy_config_created_at"`
UpdatedAt int64 `db:"upstream_proxy_config_updated_at"`
CreatedBy int64 `db:"upstream_proxy_config_created_by"`
UpdatedBy int64 `db:"upstream_proxy_config_updated_by"`
ID int64 `db:"upstream_proxy_config_id"`
RegistryID int64 `db:"upstream_proxy_config_registry_id"`
Source string `db:"upstream_proxy_config_source"`
URL string `db:"upstream_proxy_config_url"`
AuthType string `db:"upstream_proxy_config_auth_type"`
UserName string `db:"upstream_proxy_config_user_name"`
SecretIdentifier sql.NullString `db:"upstream_proxy_config_secret_identifier"`
SecretSpaceID sql.NullInt32 `db:"upstream_proxy_config_secret_space_id"`
Token string `db:"upstream_proxy_config_token"`
CreatedAt int64 `db:"upstream_proxy_config_created_at"`
UpdatedAt int64 `db:"upstream_proxy_config_updated_at"`
CreatedBy int64 `db:"upstream_proxy_config_created_by"`
UpdatedBy int64 `db:"upstream_proxy_config_updated_by"`
}

type upstreamProxyDB struct {
Expand All @@ -75,8 +75,8 @@ type upstreamProxyDB struct {
RepoURL string `db:"repo_url"`
RepoAuthType string `db:"repo_auth_type"`
UserName string `db:"user_name"`
SecretIdentifier string `db:"secret_identifier"`
SecretSpaceID int `db:"secret_space_id"`
SecretIdentifier sql.NullString `db:"secret_identifier"`
SecretSpaceID sql.NullInt32 `db:"secret_space_id"`
Token string `db:"token"`
CreatedAt int64 `db:"created_at"`
UpdatedAt int64 `db:"updated_at"`
Expand Down Expand Up @@ -360,8 +360,8 @@ func (r UpstreamproxyDao) mapToInternalUpstreamProxy(
URL: in.URL,
AuthType: in.AuthType,
UserName: in.UserName,
SecretIdentifier: in.SecretIdentifier,
SecretSpaceID: in.SecretSpaceID,
SecretIdentifier: util.GetEmptySQLString(in.SecretIdentifier),
SecretSpaceID: util.GetEmptySQLInt32(in.SecretSpaceID),
Token: in.Token,
CreatedAt: in.CreatedAt.UnixMilli(),
UpdatedAt: in.UpdatedAt.UnixMilli(),
Expand Down
35 changes: 35 additions & 0 deletions registry/app/store/database/util/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import (
"database/sql"

"github.com/harness/gitness/registry/app/pkg/commons"
)

func GetEmptySQLString(str string) sql.NullString {
if commons.IsEmpty(str) {
return sql.NullString{String: str, Valid: false}
}
return sql.NullString{String: str, Valid: true}
}

func GetEmptySQLInt32(i int) sql.NullInt32 {
if i == 0 {
return sql.NullInt32{Int32: int32(i), Valid: false}
}
return sql.NullInt32{Int32: int32(i), Valid: true}
}
5 changes: 3 additions & 2 deletions registry/types/upstream_proxy_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package types

import (
"database/sql"
"time"

"github.com/harness/gitness/registry/app/api/openapi/contracts/artifact"
Expand Down Expand Up @@ -50,8 +51,8 @@ type UpstreamProxy struct {
RepoURL string
RepoAuthType string
UserName string
SecretIdentifier string
SecretSpaceID int
SecretIdentifier sql.NullString
SecretSpaceID sql.NullInt32
Token string
CreatedAt time.Time
UpdatedAt time.Time
Expand Down

0 comments on commit d52a142

Please sign in to comment.