Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate access token for local artifactory tests #10

Merged
merged 5 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion local-rt-setup/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/jfrog/jfrog-testing-infra/local-rt-setup

go 1.17
go 1.19

require github.com/mholt/archiver/v3 v3.5.1

Expand Down
48 changes: 43 additions & 5 deletions local-rt-setup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"mime"
"net/http"
Expand All @@ -29,6 +28,8 @@ const localArtifactoryUrl = "http://localhost:8081/artifactory/"
const defaultUsername = "admin"
const defaultPassword = "password"
const defaultVersion = "[RELEASE]"
const tokenJson = "token.json"
const generateTokenJson = "generate.token.json"

func main() {
err := setupLocalArtifactory()
Expand Down Expand Up @@ -99,12 +100,18 @@ func setupLocalArtifactory() (err error) {
} else {
binDir = filepath.Join(jfrogHome, "artifactory", "app", "bin")
}

err = triggerTokenCreation(jfrogHome)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should check the version and if it is below 7.38.4, skip this phase

if err != nil {
return err
}

err = startArtifactory(binDir)
if err != nil {
return err
}

err = waitForArtifactorySuccessfulPing()
err = waitForArtifactorySuccessfulPing(jfrogHome)
if err != nil {
return err
}
Expand All @@ -119,7 +126,7 @@ func setupLocalArtifactory() (err error) {

// Rename the directory that was extracted from the archive, to easily access in the rest of the script.
func renameArtifactoryDir(jfrogHome string) error {
fileInfo, err := ioutil.ReadDir(jfrogHome)
fileInfo, err := os.ReadDir(jfrogHome)
if err != nil {
return err
}
Expand Down Expand Up @@ -183,10 +190,10 @@ func startArtifactory(binDir string) error {
return cmd.Run()
}

func waitForArtifactorySuccessfulPing() error {
func waitForArtifactorySuccessfulPing(jfrogHome string) error {
log.Println("Waiting for successful connection with Artifactory...")
tryingLog := fmt.Sprintf("Trying again in %d seconds.", waitSleepIntervalSeconds)

tokenCreated := false
for timeElapsed := 0; timeElapsed < maxConnectionWaitSeconds; timeElapsed += waitSleepIntervalSeconds {
time.Sleep(time.Second * waitSleepIntervalSeconds)

Expand All @@ -205,10 +212,41 @@ func waitForArtifactorySuccessfulPing() error {
log.Printf("Artifactory response: %d. %s", response.StatusCode, tryingLog)
}
}
if !tokenCreated {
sverdlov93 marked this conversation as resolved.
Show resolved Hide resolved
tokenCreated, err = getGeneratedToken(jfrogHome)
if err != nil {
return err
}
}
}
return errors.New("could not connect to Artifactory")
}

// More info at - https://www.jfrog.com/confluence/display/JFROG/Managing+Keys#ManagingKeys-CreatinganAutomaticAdminToken
func triggerTokenCreation(jfrogHome string) error {
generateKeysDir := filepath.Join(jfrogHome, "artifactory", "var", "bootstrap", "etc", "access", "keys")
err := os.MkdirAll(generateKeysDir, os.ModePerm)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(generateKeysDir, generateTokenJson), []byte{}, 0600)
}

func extractGeneratedToken(jfrogHome string) (bool, error) {
generatedTokenPath := filepath.Join(jfrogHome, "artifactory", "var", "etc", "access", "keys", tokenJson)
exists, err := isExists(generatedTokenPath)
if err != nil || !exists {
return false, err
}

tokenData, err := os.ReadFile(generatedTokenPath)
if err != nil {
return false, err
}
err = os.WriteFile(filepath.Join(jfrogHome, tokenJson), tokenData, 0600)
return err != nil, err
}

func ping() (*http.Response, error) {
url := localArtifactoryUrl + "api/system/ping"

Expand Down