Skip to content

Commit

Permalink
Merge pull request #145 from zong-zhe/supports-tls-flag
Browse files Browse the repository at this point in the history
feat: add flag --insecure-skip-tls-verify
  • Loading branch information
Peefy committed Sep 12, 2024
2 parents 60cae64 + de1c2c5 commit b770af8
Show file tree
Hide file tree
Showing 29 changed files with 251 additions and 17 deletions.
25 changes: 13 additions & 12 deletions cmd/kcl/commands/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ const (
)

var (
quiet bool
vendor bool
update bool
git string
oci string
path string
tag string
commit string
branch string
target string
rename string
noSumCheck bool
quiet bool
vendor bool
update bool
git string
oci string
path string
tag string
commit string
branch string
target string
rename string
noSumCheck bool
insecureSkipTLSverify bool
)

// NewModCmd returns the mod command.
Expand Down
3 changes: 3 additions & 0 deletions cmd/kcl/commands/mod_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func NewModAddCmd(cli *client.KpmClient) *cobra.Command {
cmd.Flags().StringVar(&path, "path", "", "filesystem path to local dependency to add")
cmd.Flags().StringVar(&rename, "rename", "", "rename the dependency")
cmd.Flags().BoolVar(&noSumCheck, "no_sum_check", false, "do not check the checksum of the package and update kcl.mod.lock")
cmd.Flags().BoolVar(&insecureSkipTLSverify, "insecure-skip-tls-verify", false, "skip tls certificate checks for the KCL module download")

return cmd
}
Expand All @@ -86,6 +87,8 @@ func ModAdd(cli *client.KpmClient, args []string) error {
}
}()

cli.SetInsecureSkipTLSverify(insecureSkipTLSverify)

pwd, err := os.Getwd()

if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/kcl/commands/mod_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ func NewModGraphCmd(cli *client.KpmClient) *cobra.Command {
},
SilenceUsage: true,
}

cmd.Flags().BoolVar(&insecureSkipTLSverify, "insecure-skip-tls-verify", false, "skip tls certificate checks for the KCL module download")
return cmd
}

func ModGraph(cli *client.KpmClient, args []string) error {
cli.SetInsecureSkipTLSverify(insecureSkipTLSverify)
// acquire the lock of the package cache.
err := cli.AcquirePackageCacheLock()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/kcl/commands/mod_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewModMetadataCmd(cli *client.KpmClient) *cobra.Command {
}

cli.SetLogWriter(nil)
cli.SetInsecureSkipTLSverify(insecureSkipTLSverify)
kclPkg, err := cli.LoadPkgFromPath(pwd)
if err != nil {
return err
Expand Down Expand Up @@ -76,6 +77,7 @@ func NewModMetadataCmd(cli *client.KpmClient) *cobra.Command {

cmd.Flags().BoolVar(&vendor, "vendor", false, "run in vendor mode (default: false)")
cmd.Flags().BoolVar(&update, "update", false, "check the local package and update and download the local package. (default: false)")
cmd.Flags().BoolVar(&insecureSkipTLSverify, "insecure-skip-tls-verify", false, "skip tls certificate checks for the KCL module download")

return cmd
}
2 changes: 2 additions & 0 deletions cmd/kcl/commands/mod_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func NewModPullCmd(cli *client.KpmClient) *cobra.Command {
cmd.Flags().StringVar(&tag, "tag", "", "git or oci repository tag")
cmd.Flags().StringVar(&commit, "commit", "", "git repository commit")
cmd.Flags().StringVar(&branch, "branch", "", "git repository branch")
cmd.Flags().BoolVar(&insecureSkipTLSverify, "insecure-skip-tls-verify", false, "skip tls certificate checks for the KCL module download")

return cmd
}
Expand All @@ -64,6 +65,7 @@ func pull(cli *client.KpmClient, args []string, localPath string) error {
return err
}

cli.SetInsecureSkipTLSverify(insecureSkipTLSverify)
_, err = cli.Pull(
client.WithPullSource(source),
client.WithLocalPath(localPath),
Expand Down
2 changes: 2 additions & 0 deletions cmd/kcl/commands/mod_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ func NewModPushCmd(cli *client.KpmClient) *cobra.Command {

cmd.Flags().BoolVar(&vendor, "vendor", false, "run in vendor mode (default: false)")
cmd.Flags().StringVar(&target, "tar_path", "", "packaged target path that will be pushed")
cmd.Flags().BoolVar(&insecureSkipTLSverify, "insecure-skip-tls-verify", false, "skip tls certificate checks for the KCL module download")

return cmd
}

func ModPush(cli *client.KpmClient, args []string) error {
cli.SetInsecureSkipTLSverify(insecureSkipTLSverify)
localTarPath := target
ociUrl := argsGet(args, 0)

Expand Down
132 changes: 132 additions & 0 deletions cmd/kcl/commands/mod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package cmd

import (
"bytes"
"fmt"
gohttp "net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"kcl-lang.io/kpm/pkg/client"
)

func TestModCmdWithSkipTlsVerify(t *testing.T) {
var buf bytes.Buffer

mux := gohttp.NewServeMux()
mux.HandleFunc("/", func(w gohttp.ResponseWriter, r *gohttp.Request) {
buf.WriteString("Called Success\n")
fmt.Fprintln(w, "Hello, client")
})

mux.HandleFunc("/subpath/tags/list", func(w gohttp.ResponseWriter, r *gohttp.Request) {
buf.WriteString("Called Success\n")
fmt.Fprintln(w, "Hello, client")
})

mux.HandleFunc("/subpath", func(w gohttp.ResponseWriter, r *gohttp.Request) {
fmt.Fprintln(w, "Hello from subpath")
})

ts := httptest.NewTLSServer(mux)
defer ts.Close()

fmt.Printf("ts.URL: %v\n", ts.URL)
turl, err := url.Parse(ts.URL)
assert.Equal(t, err, nil)
turl.Scheme = "oci"
turl.Path = filepath.Join(turl.Path, "subpath")
fmt.Printf("turl.String(): %v\n", turl.String())

kpmcli, err := client.NewKpmClient()
assert.Equal(t, err, nil)

originalDir, err := os.Getwd()
assert.NoError(t, err)
testRootDir := filepath.Join(originalDir, "test_data")

runTest := func(testDir string, testFunc func(), beforeTestFuncs []func()) {
if testDir != "" {
err = os.Chdir(filepath.Join(testRootDir, testDir))
assert.NoError(t, err)
}

for _, beforeTestFunc := range beforeTestFuncs {
beforeTestFunc()
}

testFunc()
assert.Equal(t, buf.String(), "Called Success\n")
buf.Reset()
defer func() {
err := os.Chdir(originalDir)
assert.NoError(t, err)
}()
}

genKclModWithDep := func(depUrl string) {
fmt.Println("Executing extra function for test_mod_graph")
kclModContent := fmt.Sprintf(`[package]
name = "test_mod"
edition = "v0.10.0"
version = "0.0.1"
[dependencies]
dep1 = { oci = "%s"}
`, depUrl)
err := os.WriteFile("kcl.mod", []byte(kclModContent), 0644)
assert.NoError(t, err)
}

runTest("", func() {
fmt.Println("test_mod_pull")
cmd := NewModPullCmd(kpmcli)
cmd.SetArgs([]string{turl.String(), "--insecure-skip-tls-verify"})
_ = cmd.Execute()
}, []func(){})

runTest("test_mod_push", func() {
fmt.Println("test_mod_push")
cmd := NewModPushCmd(kpmcli)
cmd.SetArgs([]string{turl.String(), "--insecure-skip-tls-verify"})
_ = cmd.Execute()
}, []func(){})

runTest("test_mod_add", func() {
fmt.Println("test_mod_add")
cmd := NewModAddCmd(kpmcli)
cmd.SetArgs([]string{turl.String(), "--insecure-skip-tls-verify"})
_ = cmd.Execute()
}, []func(){})

runTest("test_mod_graph", func() {
fmt.Println("test_mod_graph")
cmd := NewModGraphCmd(kpmcli)
cmd.SetArgs([]string{"--insecure-skip-tls-verify"})
_ = cmd.Execute()
}, []func(){
func() { genKclModWithDep(turl.String()) },
})

runTest("test_mod_metadata", func() {
fmt.Println("test_mod_metadata")
cmd := NewModMetadataCmd(kpmcli)
cmd.SetArgs([]string{"--update", "--insecure-skip-tls-verify"})
_ = cmd.Execute()
}, []func(){
func() { genKclModWithDep(turl.String()) },
})

runTest("test_mod_update", func() {
fmt.Println("test_mod_update")
cmd := NewModUpdateCmd(kpmcli)
cmd.SetArgs([]string{"--insecure-skip-tls-verify"})
_ = cmd.Execute()
}, []func(){
func() { genKclModWithDep(turl.String()) },
})
}
2 changes: 2 additions & 0 deletions cmd/kcl/commands/mod_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewModUpdateCmd(cli *client.KpmClient) *cobra.Command {
}

cmd.Flags().BoolVar(&noSumCheck, "no_sum_check", false, "do not check the checksum of the package and update kcl.mod.lock")
cmd.Flags().BoolVar(&insecureSkipTLSverify, "insecure-skip-tls-verify", false, "skip tls certificate checks for the KCL module download")

return cmd
}
Expand All @@ -49,6 +50,7 @@ func ModUpdate(cli *client.KpmClient, args []string) error {
pkgPath = pwd
}
cli.SetNoSumCheck(noSumCheck)
cli.SetInsecureSkipTLSverify(insecureSkipTLSverify)
kclPkg, err := cli.LoadPkgFromPath(pkgPath)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/kcl/commands/registry_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewRegistryLoginCmd(cli *client.KpmClient) *cobra.Command {
if err != nil {
return err
}

cli.SetInsecureSkipTLSverify(insecureSkipTLSverify)
err = cli.LoginOci(registry, username, password)
if err != nil {
return err
Expand All @@ -50,6 +50,6 @@ func NewRegistryLoginCmd(cli *client.KpmClient) *cobra.Command {

cmd.Flags().StringVarP(&username, "username", "u", "", "registry username")
cmd.Flags().StringVarP(&password, "password", "p", "", "registry password or identity token")

cmd.Flags().BoolVar(&insecureSkipTLSverify, "insecure-skip-tls-verify", false, "skip tls verification")
return cmd
}
41 changes: 41 additions & 0 deletions cmd/kcl/commands/registry_login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"bytes"
"fmt"
gohttp "net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"kcl-lang.io/kpm/pkg/client"
)

func TestLoginCmdWithSkipTlsVerify(t *testing.T) {
var buf bytes.Buffer

mux := gohttp.NewServeMux()
mux.HandleFunc("/", func(w gohttp.ResponseWriter, r *gohttp.Request) {
buf.WriteString("Called Success\n")
fmt.Fprintln(w, "Hello, client")
})

ts := httptest.NewTLSServer(mux)
defer ts.Close()

fmt.Printf("ts.URL: %v\n", ts.URL)
turl, err := url.Parse(ts.URL)
assert.Equal(t, err, nil)
turl.Path = filepath.Join(turl.Path, "subpath")
fmt.Printf("turl.String(): %v\n", turl.String())

cli, err := client.NewKpmClient()
assert.Equal(t, err, nil)
cmd := NewRegistryLoginCmd(cli)
cmd.SetArgs([]string{turl.String(), "--username=test-user", "--password=test-pass", "--insecure-skip-tls-verify"})
err = cmd.Execute()
assert.NoError(t, err)
assert.Equal(t, buf.String(), "Called Success\nCalled Success\n")
}
6 changes: 6 additions & 0 deletions cmd/kcl/commands/test_data/test_mod_add/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "test_mod"
edition = "v0.10.0"
version = "0.0.1"


Empty file.
1 change: 1 addition & 0 deletions cmd/kcl/commands/test_data/test_mod_add/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
8 changes: 8 additions & 0 deletions cmd/kcl/commands/test_data/test_mod_graph/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "test_mod"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
dep1 = { oci = "oci://127.0.0.1:50630/subpath"}

Empty file.
1 change: 1 addition & 0 deletions cmd/kcl/commands/test_data/test_mod_graph/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
8 changes: 8 additions & 0 deletions cmd/kcl/commands/test_data/test_mod_metadata/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "test_mod"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
dep1 = { oci = "oci://127.0.0.1:50630/subpath"}

Empty file.
1 change: 1 addition & 0 deletions cmd/kcl/commands/test_data/test_mod_metadata/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
4 changes: 4 additions & 0 deletions cmd/kcl/commands/test_data/test_mod_push/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "test_mod"
edition = "v0.10.0"
version = "0.0.1"
Empty file.
1 change: 1 addition & 0 deletions cmd/kcl/commands/test_data/test_mod_push/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
8 changes: 8 additions & 0 deletions cmd/kcl/commands/test_data/test_mod_update/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "test_mod"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
dep1 = { oci = "oci://127.0.0.1:50630/subpath"}

Empty file.
1 change: 1 addition & 0 deletions cmd/kcl/commands/test_data/test_mod_update/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Loading

0 comments on commit b770af8

Please sign in to comment.