Skip to content

Commit 032183d

Browse files
cf integration git auth (#316)
1 parent 7603234 commit 032183d

File tree

10 files changed

+114
-10
lines changed

10 files changed

+114
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.292
1+
VERSION=v0.0.293
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/integrations.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ import (
1717
"context"
1818
"encoding/json"
1919
"fmt"
20-
"os"
21-
"strings"
22-
2320
"github.com/codefresh-io/cli-v2/pkg/log"
2421
"github.com/codefresh-io/cli-v2/pkg/store"
2522
"github.com/codefresh-io/cli-v2/pkg/util"
@@ -29,6 +26,8 @@ import (
2926
"github.com/juju/ansiterm"
3027
"github.com/spf13/cobra"
3128
"github.com/spf13/viper"
29+
"os"
30+
"strings"
3231
)
3332

3433
type (
@@ -87,6 +86,7 @@ func NewGitIntegrationCommand(client *sdk.AppProxyAPI) *cobra.Command {
8786
cmd.AddCommand(NewGitIntegrationRemoveCommand(client))
8887
cmd.AddCommand(NewGitIntegrationRegisterCommand(client))
8988
cmd.AddCommand(NewGitIntegrationDeregisterCommand(client))
89+
cmd.AddCommand(NewGitAuthCommand())
9090

9191
return cmd
9292
}
@@ -405,6 +405,39 @@ func getAppProxyClient(runtime *string, client *sdk.AppProxyAPI) func(*cobra.Com
405405
}
406406
}
407407

408+
func NewGitAuthCommand() *cobra.Command {
409+
cmd := &cobra.Command{
410+
Use: "auth",
411+
Short: "Authenticate user",
412+
RunE: func(cmd *cobra.Command, args []string) error {
413+
return RunGitAuthCommand(cmd.Context(), cmd)
414+
},
415+
}
416+
417+
return cmd
418+
}
419+
420+
func RunGitAuthCommand(ctx context.Context, cmd *cobra.Command) error {
421+
var err error
422+
user, err := cfConfig.GetCurrentContext().GetUser(ctx)
423+
if err != nil {
424+
return err
425+
}
426+
427+
accountId, err := util.CurrentAccount(user)
428+
if err != nil {
429+
return err
430+
}
431+
432+
runtimeName := cmd.Flag("runtime").Value.String()
433+
runtime, err := cfConfig.NewClient().V2().Runtime().Get(ctx, runtimeName)
434+
if err != nil {
435+
return err
436+
}
437+
438+
return util.OpenBrowserForGitLogin(*runtime.IngressHost, user.ID, accountId)
439+
}
440+
408441
func printIntegration(i interface{}, format string) error {
409442
var (
410443
data []byte

docs/commands/cli-v2_integration_git.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ cli-v2 integration git [flags]
2727

2828
* [cli-v2 integration](cli-v2_integration.md) - Manage integrations with git providers, container registries and more
2929
* [cli-v2 integration git add](cli-v2_integration_git_add.md) - Add a new git integration
30+
* [cli-v2 integration git auth](cli-v2_integration_git_auth.md) - Authenticate user
3031
* [cli-v2 integration git deregister](cli-v2_integration_git_deregister.md) - Deregister user from a git integrations
3132
* [cli-v2 integration git edit](cli-v2_integration_git_edit.md) - Edit a git integration
3233
* [cli-v2 integration git get](cli-v2_integration_git_get.md) - Retrieve a git integration
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## cli-v2 integration git auth
2+
3+
Authenticate user
4+
5+
```
6+
cli-v2 integration git auth [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for auth
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```
18+
--auth-context string Run the next command using a specific authentication context
19+
--cfconfig string Custom path for authentication contexts config file (default "/home/user")
20+
--insecure Disable certificate validation for TLS connections (e.g. to g.codefresh.io)
21+
--insecure-ingress-host Disable certificate validation of ingress host (default: false)
22+
--request-timeout duration Request timeout (default 30s)
23+
--runtime string Name of runtime to use
24+
```
25+
26+
### SEE ALSO
27+
28+
* [cli-v2 integration git](cli-v2_integration_git.md) - Manage your git integrations
29+

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.292/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.293/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.292/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.293/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/manifoldco/promptui v0.8.0
2323
github.com/mattn/go-colorable v0.1.12 // indirect
2424
github.com/openshift/api v3.9.0+incompatible
25+
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
2526
github.com/rkrmr33/checklist v0.0.5
2627
github.com/segmentio/backo-go v1.0.0 // indirect
2728
github.com/sirupsen/logrus v1.8.1

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,8 @@ github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0
10831083
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
10841084
github.com/pierrec/lz4 v2.4.1+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
10851085
github.com/pierrec/lz4 v2.5.0+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
1086+
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
1087+
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
10861088
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10871089
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10881090
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -1626,6 +1628,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
16261628
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
16271629
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
16281630
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1631+
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
16291632
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
16301633
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
16311634
golang.org/x/sys v0.0.0-20220222200937-f2425489ef4c h1:sSIdNI2Dd6vGv47bKc/xArpfxVmEz2+3j0E6I484xC4=

manifests/app-proxy/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ kind: Kustomization
33
images:
44
- name: quay.io/codefresh/cap-app-proxy
55
newName: quay.io/codefresh/cap-app-proxy
6-
newTag: 1.944.0
6+
newTag: 1.951.0
77
resources:
88
- app-proxy.deploy.yaml
99
- app-proxy.svc.yaml

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: "{{ namespace }}"
66
spec:
77
defVersion: 1.0.1
8-
version: 0.0.292
8+
version: 0.0.293
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

pkg/util/util.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
package util
1616

1717
import (
18+
"bytes"
1819
"context"
1920
"fmt"
21+
"github.com/pkg/browser"
22+
"net/url"
2023
"os"
2124
"os/signal"
2225
"regexp"
@@ -29,6 +32,7 @@ import (
2932
"github.com/codefresh-io/cli-v2/pkg/log"
3033
"github.com/codefresh-io/cli-v2/pkg/reporter"
3134
"github.com/codefresh-io/cli-v2/pkg/store"
35+
"github.com/codefresh-io/go-sdk/pkg/codefresh"
3236

3337
"k8s.io/client-go/tools/clientcmd"
3438
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
@@ -171,7 +175,7 @@ func kubeConfig() *clientcmdapi.Config {
171175
}
172176

173177
type kubeContext struct {
174-
Name string
178+
Name string
175179
Current bool
176180
}
177181

@@ -181,7 +185,7 @@ func KubeContexts() []kubeContext {
181185
i := 0
182186
for key := range conf.Contexts {
183187
contexts[i] = kubeContext{
184-
Name: key,
188+
Name: key,
185189
Current: key == conf.CurrentContext,
186190
}
187191
i += 1
@@ -218,6 +222,39 @@ func KubeCurrentServer() (string, error) {
218222
return KubeServerByContextName("")
219223
}
220224

225+
func CurrentAccount(user *codefresh.User) (string, error) {
226+
for i := range user.Accounts {
227+
if user.Accounts[i].Name == user.ActiveAccountName {
228+
return user.Accounts[i].ID, nil
229+
}
230+
}
231+
return "", fmt.Errorf("account id for \"%s\" not found", user.ActiveAccountName)
232+
}
233+
234+
func OpenBrowserForGitLogin(ingressHost string, user string, account string) error {
235+
var b bytes.Buffer
236+
if !strings.HasPrefix(ingressHost, "http") {
237+
b.WriteString("https://")
238+
}
239+
b.WriteString(ingressHost)
240+
b.WriteString("/app-proxy/api/git-auth/github?userId=" + user + "&accountId=" + account)
241+
242+
url, err := url.Parse(b.String())
243+
if err != nil {
244+
return err
245+
}
246+
247+
err = browser.OpenURL(url.String())
248+
if err != nil {
249+
return err
250+
}
251+
252+
fmt.Println("Follow instructions in web browser")
253+
time.Sleep(2 * time.Second)
254+
255+
return nil
256+
}
257+
221258
func KubeContextNameByServer(server string) (string, error) {
222259
conf := kubeConfig()
223260
for contextName, context := range conf.Contexts {

0 commit comments

Comments
 (0)