Skip to content

Commit

Permalink
Support API Key instead of Bearer Token
Browse files Browse the repository at this point in the history
  • Loading branch information
puthrayaharness committed Jan 4, 2023
1 parent 4bc7bd4 commit 3b61861
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
10 changes: 5 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ To migrate an application
harness-upgrade app
```

We use Bearer auth token to make API calls. The token can be provided in the step-by-step guide in the prompt or as below
We use API keys created in NextGen to make API calls. The token can be provided in the step-by-step guide in the prompt or as below

```shell
HARNESS_MIGRATOR_AUTH=token harness-upgrade
HARNESS_MIGRATOR_AUTH=apiKey harness-upgrade
```

OR
```shell
export HARNESS_MIGRATOR_AUTH=token
export HARNESS_MIGRATOR_AUTH=apiKey
harness-upgrade
```

### Migrating with a single command
```shell
HARNESS_MIGRATOR_AUTH=token harness-upgrade --project PROJECT --org ORG --account ACCOUNT_ID --secret SCOPE --connector SCOPE --env ENV
HARNESS_MIGRATOR_AUTH=apiKey harness-upgrade --project PROJECT --org ORG --account ACCOUNT_ID --secret SCOPE --connector SCOPE --env ENV
```

To migrate an application

```shell
HARNESS_MIGRATOR_AUTH=token harness-upgrade app APP_ID --project PROJECT --org ORG --account ACCOUNT_ID --secret SCOPE --connector SCOPE --env ENV
HARNESS_MIGRATOR_AUTH=apiKey harness-upgrade app APP_ID --project PROJECT --org ORG --account ACCOUNT_ID --secret SCOPE --connector SCOPE --env ENV
```

| Flag | Details |
Expand Down
12 changes: 8 additions & 4 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/AlecAivazis/survey/v2"
log "github.com/sirupsen/logrus"
Expand All @@ -20,7 +21,7 @@ const (
var urlMap = map[string]string{
Prod: "https://app.harness.io/gateway/ng-migration",
QA: "https://qa.harness.io/gateway/ng-migration",
Dev: "https://localhost:9090",
Dev: "https://localhost:9080",
}

func TextInput(question string) string {
Expand Down Expand Up @@ -71,12 +72,15 @@ func PostReq(reqUrl string, auth string, body interface{}) ([]byte, error) {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+auth)
req.Header.Set("x-api-key", auth)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New("received non 200 response code. The response code was " + string(rune(resp.StatusCode)))
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
Expand All @@ -91,13 +95,13 @@ func PostReq(reqUrl string, auth string, body interface{}) ([]byte, error) {
}

func GetUrl(environment string, path string, accountId string) string {
return fmt.Sprintf("%s/api/ng-migration/%s?accountId=%s", urlMap[environment], path, accountId)
return fmt.Sprintf("%s/api/ng-migration/%s?accountIdentifier=%s", urlMap[environment], path, accountId)
}

func CreateEntity(url string, auth string, body RequestBody) {
resp, err := PostReq(url, auth, body)
if err != nil {
log.Fatalln("There was error while migrating. Exiting...")
log.Fatalln("There was error while migrating. Exiting...", err)
}

respBody := MigrationResponseBody{}
Expand Down
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"crypto/tls"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"net/http"
"os"
)

Expand All @@ -23,6 +25,7 @@ var migrationReq = struct {
AppId string `survey:"appId"`
Debug bool `survey:"debug"`
Json bool `survey:"json"`
AllowInsecureReq bool `survey:"insecure"`
}{}

func getReqBody(entityType EntityType, filter Filter) RequestBody {
Expand All @@ -43,14 +46,18 @@ func PromptDefaultInputs() bool {
// Check if auth is provided. If not provided then request for one
migrationReq.Auth = os.Getenv("HARNESS_MIGRATOR_AUTH")
if len(migrationReq.Auth) == 0 {
migrationReq.Auth = TextInput("The environment variable 'HARNESS_MIGRATOR_AUTH' is not set. What is the auth token?")
migrationReq.Auth = TextInput("The environment variable 'HARNESS_MIGRATOR_AUTH' is not set. What is the api key?")
}

if len(migrationReq.Environment) == 0 {
promptConfirm = true
migrationReq.Environment = SelectInput("Which environment?", []string{"Dev", "QA", "Prod"}, Dev)
}

if migrationReq.Environment == "Dev" || migrationReq.AllowInsecureReq {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}

if len(migrationReq.Account) == 0 {
promptConfirm = true
migrationReq.Account = TextInput("Account that you wish to migrate:")
Expand Down Expand Up @@ -250,6 +257,11 @@ func main() {
Usage: "project `identifier` in next gen",
Destination: &migrationReq.ProjectIdentifier,
},
&cli.BoolFlag{
Name: "insecure",
Usage: "Allow insecure API requests. This is automatically set to true if environment is `Dev`",
Destination: &migrationReq.AllowInsecureReq,
},
&cli.BoolFlag{
Name: "debug",
Usage: "print debug level logs",
Expand Down

0 comments on commit 3b61861

Please sign in to comment.