Skip to content

Commit

Permalink
Merge branch 'dev' into feature/shift-left-sec
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalbe4 authored Apr 24, 2024
2 parents 8d06850 + dac0f92 commit eb83ea7
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 74 deletions.
1 change: 1 addition & 0 deletions artifactory/commands/generic/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func getUploadParams(f *spec.File, configuration *utils.UploadConfiguration, bui
uploadParams.MinChecksumDeploy = configuration.MinChecksumDeploySize
uploadParams.MinSplitSize = configuration.MinSplitSizeMB * rtServicesUtils.SizeMiB
uploadParams.SplitCount = configuration.SplitCount
uploadParams.ChunkSize = configuration.ChunkSizeMB * rtServicesUtils.SizeMiB
uploadParams.AddVcsProps = addVcsProps
uploadParams.BuildProps = buildProps
uploadParams.Archive = f.Archive
Expand Down
2 changes: 1 addition & 1 deletion artifactory/commands/transferfiles/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (clcm *ChunksLifeCycleManager) StoreStaleChunks(stateManager *state.Transfe
for _, file := range uploadedChunkData.ChunkFiles {
var sizeStr string
if file.Size > 0 {
sizeStr = " (" + utils.ConvertIntToStorageSizeString(file.Size) + ")"
sizeStr = " (" + serviceUtils.ConvertIntToStorageSizeString(file.Size) + ")"
}
staleNodeChunk.Files = append(staleNodeChunk.Files, path.Join(file.Repo, file.Path, file.Name)+sizeStr)
}
Expand Down
22 changes: 0 additions & 22 deletions artifactory/utils/storageinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package utils
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -179,24 +178,3 @@ func convertStorageSizeStringToBytes(sizeStr string) (int64, error) {
}
return int64(sizeInBytes), nil
}

func ConvertIntToStorageSizeString(num int64) string {
if num > utils.SizeTiB {
newNum := float64(num) / float64(utils.SizeTiB)
stringNum := fmt.Sprintf("%.1f", newNum)
return stringNum + "TB"
}
if num > utils.SizeGiB {
newNum := float64(num) / float64(utils.SizeGiB)
stringNum := fmt.Sprintf("%.1f", newNum)
return stringNum + "GB"
}
if num > utils.SizeMiB {
newNum := float64(num) / float64(utils.SizeMiB)
stringNum := fmt.Sprintf("%.1f", newNum)
return stringNum + "MB"
}
newNum := float64(num) / float64(utils.SizeKib)
stringNum := fmt.Sprintf("%.1f", newNum)
return stringNum + "KB"
}
18 changes: 0 additions & 18 deletions artifactory/utils/storageinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,3 @@ func getStorageInfoResponse(t *testing.T, w http.ResponseWriter, r *http.Request
assert.NoError(t, err)
}
}

func TestConvertIntToStorageSizeString(t *testing.T) {
tests := []struct {
num int
output string
}{
{12546, "12.3KB"},
{148576, "145.1KB"},
{2587985, "2.5MB"},
{12896547, "12.3MB"},
{12896547785, "12.0GB"},
{5248965785422365, "4773.9TB"},
}

for _, test := range tests {
assert.Equal(t, test.output, ConvertIntToStorageSizeString(int64(test.num)))
}
}
1 change: 1 addition & 0 deletions artifactory/utils/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type UploadConfiguration struct {
ExplodeArchive bool
SplitCount int
MinSplitSizeMB int64
ChunkSizeMB int64
}

func GetMinChecksumDeploySize() (int64, error) {
Expand Down
51 changes: 33 additions & 18 deletions common/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ package commands
import (
"errors"
"fmt"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"sync"

"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
Expand All @@ -14,12 +21,6 @@ import (
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"sync"
)

type ConfigAction string
Expand Down Expand Up @@ -105,17 +106,7 @@ func (cc *ConfigCommand) SetDetails(details *config.ServerDetails) *ConfigComman

func (cc *ConfigCommand) Run() (err error) {
log.Debug("Locking config file to run config " + cc.cmdType + " command.")
mutex.Lock()
defer func() {
mutex.Unlock()
log.Debug("Config " + cc.cmdType + " command completed successfully. config file is released.")
}()

lockDirPath, err := coreutils.GetJfrogConfigLockDir()
if err != nil {
return
}
unlockFunc, err := lock.CreateLock(lockDirPath)
unlockFunc, err := lockConfig()
// Defer the lockFile.Unlock() function before throwing a possible error to avoid deadlock situations.
defer func() {
err = errors.Join(err, unlockFunc())
Expand Down Expand Up @@ -551,7 +542,21 @@ func ShowConfig(serverName string) error {
return nil
}

func Import(configTokenString string) error {
func lockConfig() (unlockFunc func() error, err error) {
mutex.Lock()
defer func() {
mutex.Unlock()
log.Debug("config file is released.")
}()

lockDirPath, err := coreutils.GetJfrogConfigLockDir()
if err != nil {
return
}
return lock.CreateLock(lockDirPath)
}

func Import(configTokenString string) (err error) {
serverDetails, err := config.Import(configTokenString)
if err != nil {
return err
Expand All @@ -561,6 +566,16 @@ func Import(configTokenString string) error {
details: serverDetails,
serverId: serverDetails.ServerId,
}

log.Debug("Locking config file to run config import command.")
unlockFunc, err := lockConfig()
// Defer the lockFile.Unlock() function before throwing a possible error to avoid deadlock situations.
defer func() {
err = errors.Join(err, unlockFunc())
}()
if err != nil {
return err
}
return configCommand.config()
}

Expand Down
29 changes: 28 additions & 1 deletion common/commands/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import (
"github.com/stretchr/testify/assert"
)

const testServerId = "test"
const (
testServerId = "test"
// #nosec G101 -- False positive - no hardcoded credentials.
// jfrog-ignore - not a real token
acmeConfigToken = "eyJ2ZXJzaW9uIjoyLCJ1cmwiOiJodHRwczovL2FjbWUuamZyb2cuaW8vIiwiYXJ0aWZhY3RvcnlVcmwiOiJodHRwczovL2FjbWUuamZyb2cuaW8vYXJ0aWZhY3RvcnkvIiwiZGlzdHJpYnV0aW9uVXJsIjoiaHR0cHM6Ly9hY21lLmpmcm9nLmlvL2Rpc3RyaWJ1dGlvbi8iLCJ4cmF5VXJsIjoiaHR0cHM6Ly9hY21lLmpmcm9nLmlvL3hyYXkvIiwibWlzc2lvbkNvbnRyb2xVcmwiOiJodHRwczovL2FjbWUuamZyb2cuaW8vbWMvIiwicGlwZWxpbmVzVXJsIjoiaHR0cHM6Ly9hY21lLmpmcm9nLmlvL3BpcGVsaW5lcy8iLCJ1c2VyIjoiYWRtaW4iLCJwYXNzd29yZCI6InBhc3N3b3JkIiwidG9rZW5SZWZyZXNoSW50ZXJ2YWwiOjYwLCJzZXJ2ZXJJZCI6ImFjbWUifQ=="
)

func init() {
log.SetDefaultLogger()
Expand Down Expand Up @@ -317,6 +322,28 @@ func TestKeyDecryptionError(t *testing.T) {
assert.ErrorContains(t, err, "cannot decrypt config")
}

func TestImport(t *testing.T) {
// Create temp jfrog home
cleanUpJfrogHome, err := utilsTests.SetJfrogHome()
assert.NoError(t, err)
defer cleanUpJfrogHome()

// Import config token
assert.NoError(t, Import(acmeConfigToken))
serverDetails, err := GetConfig("acme", true)
assert.NoError(t, err)

// Verify that the configuration was imported correctly
assert.Equal(t, "https://acme.jfrog.io/", serverDetails.GetUrl())
assert.Equal(t, "https://acme.jfrog.io/artifactory/", serverDetails.GetArtifactoryUrl())
assert.Equal(t, "https://acme.jfrog.io/distribution/", serverDetails.GetDistributionUrl())
assert.Equal(t, "https://acme.jfrog.io/xray/", serverDetails.GetXrayUrl())
assert.Equal(t, "https://acme.jfrog.io/mc/", serverDetails.GetMissionControlUrl())
assert.Equal(t, "https://acme.jfrog.io/pipelines/", serverDetails.GetPipelinesUrl())
assert.Equal(t, "admin", serverDetails.GetUser())
assert.Equal(t, "password", serverDetails.GetPassword())
}

func testExportImport(t *testing.T, inputDetails *config.ServerDetails) {
configToken, err := config.Export(inputDetails)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
)

// replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240408071430-62ee0279ac58
replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240424133643-5bf715f66eac

// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go dev

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ github.com/jfrog/build-info-go v1.9.26 h1:1Ddc6+Ecvhc+UMnKhRVG1jGM6fYNwA49207azT
github.com/jfrog/build-info-go v1.9.26/go.mod h1:8T7/ajM9aGshvgpwCtXwIFpyF/R6CEn4W+/FLryNXWw=
github.com/jfrog/gofrog v1.7.1 h1:ME1Meg4hukAT/7X6HUQCVSe4DNjMZACCP8aCY37EW/w=
github.com/jfrog/gofrog v1.7.1/go.mod h1:X7bjfWoQDN0Z4FQGbE91j3gbPP7Urwzm4Z8tkvrlbRI=
github.com/jfrog/jfrog-client-go v1.40.1 h1:ISSSV7/IUS8R+QCPfH2lVKLburbv2Xn07fvNyDc17rI=
github.com/jfrog/jfrog-client-go v1.40.1/go.mod h1:FprEW0Sqhj6ZSFTFk9NCni+ovFAYMA3zCBmNX4hGXgQ=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240424133643-5bf715f66eac h1:e/QfkFN/Qtkn1rBg/p7JCOMFLmE2EgfPBx57m2yIF1k=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240424133643-5bf715f66eac/go.mod h1:FprEW0Sqhj6ZSFTFk9NCni+ovFAYMA3zCBmNX4hGXgQ=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand Down
21 changes: 10 additions & 11 deletions utils/progressbar/progressbarmng.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package progressbar

import (
golangLog "log"
"math"
"os"
"strconv"
"strings"
"sync"
"time"

"github.com/gookit/color"
artifactoryutils "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
corelog "github.com/jfrog/jfrog-cli-core/v2/utils/log"
servicesUtils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/vbauerster/mpb/v7"
"github.com/vbauerster/mpb/v7/decor"
"golang.org/x/term"
golangLog "log"
"math"
"os"
"strconv"
"strings"
"sync"
"time"
)

const (
Expand Down Expand Up @@ -98,8 +97,8 @@ func (bm *ProgressBarMng) newDoubleValueProgressBar(getVal func() (firstNumerato
if err != nil {
log.Error(err)
}
s1 := artifactoryutils.ConvertIntToStorageSizeString(*firstNumerator)
s2 := artifactoryutils.ConvertIntToStorageSizeString(*firstDenominator)
s1 := servicesUtils.ConvertIntToStorageSizeString(*firstNumerator)
s2 := servicesUtils.ConvertIntToStorageSizeString(*firstDenominator)
return color.Green.Render(s1 + "/" + s2)
}), decor.Name(" "+secondValueLine+": "), decor.Any(func(statistics decor.Statistics) string {
_, _, secondNumerator, secondDenominator, err := getVal()
Expand Down

0 comments on commit eb83ea7

Please sign in to comment.