Unverified Commit dd57ff2b authored by Oren Shomron's avatar Oren Shomron Committed by GitHub
Browse files

Merge pull request #832 from shomron/issue-726-tls-skip-verify-init

Respect --tls-skip-verify for ks init, ks env * commands
parents c7b94fe5 79e6651c
...@@ -17,6 +17,7 @@ package actions ...@@ -17,6 +17,7 @@ package actions
import ( import (
"fmt" "fmt"
"net/http"
"github.com/ksonnet/ksonnet/pkg/app" "github.com/ksonnet/ksonnet/pkg/app"
"github.com/ksonnet/ksonnet/pkg/lib" "github.com/ksonnet/ksonnet/pkg/lib"
...@@ -37,7 +38,8 @@ type EnvUpdate struct { ...@@ -37,7 +38,8 @@ type EnvUpdate struct {
app app.App app app.App
envName string envName string
genLibFn func(app.App, string, string) error httpClient *http.Client
genLibFn func(app.App, string, string, *http.Client) error
} }
// RunEnvUpdate runs `env update` // RunEnvUpdate runs `env update`
...@@ -48,7 +50,8 @@ func newEnvUpdate(m map[string]interface{}) (*EnvUpdate, error) { ...@@ -48,7 +50,8 @@ func newEnvUpdate(m map[string]interface{}) (*EnvUpdate, error) {
app: ol.LoadApp(), app: ol.LoadApp(),
envName: ol.LoadString(OptionEnvName), envName: ol.LoadString(OptionEnvName),
genLibFn: genLib, httpClient: ol.LoadHTTPClient(),
genLibFn: genLib,
} }
if ol.err != nil { if ol.err != nil {
...@@ -71,12 +74,12 @@ func (eu *EnvUpdate) run() error { ...@@ -71,12 +74,12 @@ func (eu *EnvUpdate) run() error {
return err return err
} }
return eu.genLibFn(eu.app, k8sSpecFlag, libPath) return eu.genLibFn(eu.app, k8sSpecFlag, libPath, eu.httpClient)
} }
func genLib(a app.App, k8sSpecFlag, libPath string) error { func genLib(a app.App, k8sSpecFlag, libPath string, httpClient *http.Client) error {
libManager, err := lib.NewManager(k8sSpecFlag, a.Fs(), libPath) libManager, err := lib.NewManager(k8sSpecFlag, a.Fs(), libPath, httpClient)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package actions package actions
import ( import (
"net/http"
"testing" "testing"
"github.com/ksonnet/ksonnet/pkg/app" "github.com/ksonnet/ksonnet/pkg/app"
...@@ -41,7 +42,7 @@ func TestEnvUpdate(t *testing.T) { ...@@ -41,7 +42,7 @@ func TestEnvUpdate(t *testing.T) {
a, err := newEnvUpdate(in) a, err := newEnvUpdate(in)
require.NoError(t, err) require.NoError(t, err)
a.genLibFn = func(_ app.App, k8sSpecFlag, libPath string) error { a.genLibFn = func(_ app.App, k8sSpecFlag, libPath string, httpClient *http.Client) error {
assert.Equal(t, "version:v1.8.9", k8sSpecFlag) assert.Equal(t, "version:v1.8.9", k8sSpecFlag)
assert.Equal(t, "/app/lib/v1.8.9", libPath) assert.Equal(t, "/app/lib/v1.8.9", libPath)
return nil return nil
......
...@@ -16,9 +16,12 @@ ...@@ -16,9 +16,12 @@
package actions package actions
import ( import (
"net/http"
"github.com/ksonnet/ksonnet/pkg/app" "github.com/ksonnet/ksonnet/pkg/app"
"github.com/ksonnet/ksonnet/pkg/appinit" "github.com/ksonnet/ksonnet/pkg/appinit"
"github.com/ksonnet/ksonnet/pkg/registry" "github.com/ksonnet/ksonnet/pkg/registry"
"github.com/ksonnet/ksonnet/pkg/util/github"
"github.com/spf13/afero" "github.com/spf13/afero"
) )
...@@ -37,11 +40,11 @@ func RunInit(m map[string]interface{}) error { ...@@ -37,11 +40,11 @@ func RunInit(m map[string]interface{}) error {
return i.Run() return i.Run()
} }
type appLoadFn func(fs afero.Fs, root string, skipFindRoot bool) (app.App, error) type appLoadFn func(fs afero.Fs, httpClient *http.Client, root string, skipFindRoot bool) (app.App, error)
type appInitFn func(fs afero.Fs, name, rootPath, envName, k8sSpecFlag, serverURI, namespace string, registries []registry.Registry) error type appInitFn func(fs afero.Fs, httpClient *http.Client, name, rootPath, envName, k8sSpecFlag, serverURI, namespace string, registries []registry.Registry) error
type initIncubatorFn func(app.App) (registry.Registry, error) type initIncubatorFn func(app.App, *http.Client) (registry.Registry, error)
// Init creates a component namespace // Init creates a component namespace
type Init struct { type Init struct {
...@@ -57,6 +60,8 @@ type Init struct { ...@@ -57,6 +60,8 @@ type Init struct {
appInitFn appInitFn appInitFn appInitFn
appLoadFn appLoadFn appLoadFn appLoadFn
initIncubatorFn initIncubatorFn initIncubatorFn initIncubatorFn
httpClient *http.Client
} }
// NewInit creates an instance of Init. // NewInit creates an instance of Init.
...@@ -76,6 +81,8 @@ func NewInit(m map[string]interface{}) (*Init, error) { ...@@ -76,6 +81,8 @@ func NewInit(m map[string]interface{}) (*Init, error) {
appInitFn: appinit.Init, appInitFn: appinit.Init,
appLoadFn: app.Load, appLoadFn: app.Load,
initIncubatorFn: initIncubator, initIncubatorFn: initIncubator,
httpClient: ol.LoadHTTPClient(),
} }
if ol.err != nil { if ol.err != nil {
...@@ -90,12 +97,12 @@ func (i *Init) Run() error { ...@@ -90,12 +97,12 @@ func (i *Init) Run() error {
var registries []registry.Registry var registries []registry.Registry
if !i.skipDefaultRegistries { if !i.skipDefaultRegistries {
a, err := i.appLoadFn(i.fs, i.rootPath, true) a, err := i.appLoadFn(i.fs, i.httpClient, i.rootPath, true)
if err != nil { if err != nil {
return err return err
} }
gh, err := i.initIncubatorFn(a) gh, err := i.initIncubatorFn(a, i.httpClient)
if err != nil { if err != nil {
return err return err
} }
...@@ -105,6 +112,7 @@ func (i *Init) Run() error { ...@@ -105,6 +112,7 @@ func (i *Init) Run() error {
return i.appInitFn( return i.appInitFn(
i.fs, i.fs,
i.httpClient,
i.name, i.name,
i.rootPath, i.rootPath,
i.envName, i.envName,
...@@ -115,12 +123,14 @@ func (i *Init) Run() error { ...@@ -115,12 +123,14 @@ func (i *Init) Run() error {
) )
} }
func initIncubator(a app.App) (registry.Registry, error) { func initIncubator(a app.App, httpClient *http.Client) (registry.Registry, error) {
gh := github.NewGitHub(httpClient)
return registry.NewGitHub( return registry.NewGitHub(
a, a,
&app.RegistryConfig{ &app.RegistryConfig{
Name: "incubator", Name: "incubator",
Protocol: string(registry.ProtocolGitHub), Protocol: string(registry.ProtocolGitHub),
URI: defaultIncubatorURI, URI: defaultIncubatorURI,
}) }, registry.GitHubClient(gh))
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package actions package actions
import ( import (
"net/http"
"testing" "testing"
"github.com/ksonnet/ksonnet/pkg/app" "github.com/ksonnet/ksonnet/pkg/app"
...@@ -65,12 +66,13 @@ func TestInit(t *testing.T) { ...@@ -65,12 +66,13 @@ func TestInit(t *testing.T) {
OptionServer: aServerURI, OptionServer: aServerURI,
OptionNamespace: aNamespace, OptionNamespace: aNamespace,
OptionSkipDefaultRegistries: tc.skipRegistries, OptionSkipDefaultRegistries: tc.skipRegistries,
OptionTLSSkipVerify: false,
} }
a, err := NewInit(in) a, err := NewInit(in)
require.NoError(t, err) require.NoError(t, err)
a.appInitFn = func(fs afero.Fs, name, rootPath, envName, k8sSpecFlag, serverURI, namespace string, registries []registry.Registry) error { a.appInitFn = func(fs afero.Fs, httpClient *http.Client, name, rootPath, envName, k8sSpecFlag, serverURI, namespace string, registries []registry.Registry) error {
assert.Equal(t, aFs, fs) assert.Equal(t, aFs, fs)
assert.Equal(t, aName, name) assert.Equal(t, aName, name)
assert.Equal(t, aRootPath, rootPath) assert.Equal(t, aRootPath, rootPath)
...@@ -93,11 +95,11 @@ func TestInit(t *testing.T) { ...@@ -93,11 +95,11 @@ func TestInit(t *testing.T) {
return nil return nil
} }
a.appLoadFn = func(fs afero.Fs, root string, skipFindRoot bool) (app.App, error) { a.appLoadFn = func(fs afero.Fs, httpClient *http.Client, root string, skipFindRoot bool) (app.App, error) {
return appMock, nil return appMock, nil
} }
a.initIncubatorFn = func(a app.App) (registry.Registry, error) { a.initIncubatorFn = func(a app.App, httpClient *http.Client) (registry.Registry, error) {
r := &rmocks.Registry{} r := &rmocks.Registry{}
r.On("Protocol").Return(registry.ProtocolGitHub) r.On("Protocol").Return(registry.ProtocolGitHub)
r.On("URI").Return("github.com/ksonnet/parts/tree/master/incubator") r.On("URI").Return("github.com/ksonnet/parts/tree/master/incubator")
......
...@@ -16,11 +16,11 @@ ...@@ -16,11 +16,11 @@
package app package app
import ( import (
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
"github.com/ksonnet/ksonnet/pkg/lib"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/afero" "github.com/spf13/afero"
...@@ -49,9 +49,6 @@ var ( ...@@ -49,9 +49,6 @@ var (
DefaultFilePermissions = os.FileMode(0644) DefaultFilePermissions = os.FileMode(0644)
// DefaultFolderPermissions are the default permissions for a folder. // DefaultFolderPermissions are the default permissions for a folder.
DefaultFolderPermissions = os.FileMode(0755) DefaultFolderPermissions = os.FileMode(0755)
// LibUpdater updates ksonnet lib versions.
LibUpdater = updateLibData
) )
// App is a ksonnet application. // App is a ksonnet application.
...@@ -70,6 +67,8 @@ type App interface { ...@@ -70,6 +67,8 @@ type App interface {
EnvironmentParams(name string) (string, error) EnvironmentParams(name string) (string, error)
// Fs is the app's afero Fs. // Fs is the app's afero Fs.
Fs() afero.Fs Fs() afero.Fs
// HTTPClient is the app's http client
HTTPClient() *http.Client
// CheckUpgrade checks whether an app should be upgraded. // CheckUpgrade checks whether an app should be upgraded.
CheckUpgrade() (bool, error) CheckUpgrade() (bool, error)
// LibPath returns the path of the lib for an environment. // LibPath returns the path of the lib for an environment.
...@@ -102,7 +101,7 @@ type App interface { ...@@ -102,7 +101,7 @@ type App interface {
} }
// Load loads the application configuration. // Load loads the application configuration.
func Load(fs afero.Fs, cwd string, skipFindRoot bool) (App, error) { func Load(fs afero.Fs, httpClient *http.Client, cwd string, skipFindRoot bool) (App, error) {
log := log.WithField("action", "app.Load") log := log.WithField("action", "app.Load")
appRoot := cwd appRoot := cwd
if !skipFindRoot { if !skipFindRoot {
...@@ -116,7 +115,7 @@ func Load(fs afero.Fs, cwd string, skipFindRoot bool) (App, error) { ...@@ -116,7 +115,7 @@ func Load(fs afero.Fs, cwd string, skipFindRoot bool) (App, error) {
spec, err := read(fs, appRoot) spec, err := read(fs, appRoot)
if os.IsNotExist(err) { if os.IsNotExist(err) {
// During `ks init`, app.yaml will not yet exist - generate a new one. // During `ks init`, app.yaml will not yet exist - generate a new one.
return NewApp010(fs, appRoot), nil return NewApp010(fs, appRoot, httpClient), nil
} }
if err != nil { if err != nil {
return nil, errors.Wrap(err, "reading app configuration") return nil, errors.Wrap(err, "reading app configuration")
...@@ -126,13 +125,13 @@ func Load(fs afero.Fs, cwd string, skipFindRoot bool) (App, error) { ...@@ -126,13 +125,13 @@ func Load(fs afero.Fs, cwd string, skipFindRoot bool) (App, error) {
default: default:
return nil, errors.Errorf("unknown apiVersion %q in %s", spec.APIVersion, appYamlName) return nil, errors.Errorf("unknown apiVersion %q in %s", spec.APIVersion, appYamlName)
case "0.0.1": case "0.0.1":
return NewApp001(fs, appRoot), nil return NewApp001(fs, appRoot, httpClient), nil
case "0.1.0", "0.2.0": case "0.1.0", "0.2.0":
// TODO TODO // TODO TODO
// 0.1.0 will auto-upgraded to 0.2.0. 0.1.0 is read-compatible with // 0.1.0 will auto-upgraded to 0.2.0. 0.1.0 is read-compatible with
// 0.2.0, but will be persisted back as 0.2.0. This behavior will be // 0.2.0, but will be persisted back as 0.2.0. This behavior will be
// subsequently changed with new upgrade framework. // subsequently changed with new upgrade framework.
a := NewApp010(fs, appRoot) a := NewApp010(fs, appRoot, httpClient)
log.Debugf("Interpreting app version as latest (0.2.0)", a.baseApp) log.Debugf("Interpreting app version as latest (0.2.0)", a.baseApp)
a.config.APIVersion = "0.2.0" a.config.APIVersion = "0.2.0"
a.baseApp.config.APIVersion = "0.2.0" a.baseApp.config.APIVersion = "0.2.0"
...@@ -140,19 +139,6 @@ func Load(fs afero.Fs, cwd string, skipFindRoot bool) (App, error) { ...@@ -140,19 +139,6 @@ func Load(fs afero.Fs, cwd string, skipFindRoot bool) (App, error) {
} }
} }
func updateLibData(fs afero.Fs, k8sSpecFlag, libPath string) (string, error) {
lm, err := lib.NewManager(k8sSpecFlag, fs, libPath)
if err != nil {
return "", err
}
if err := lm.GenerateLibData(); err != nil {
return "", err
}
return lm.K8sVersion, nil
}
func app010LibPath(root string) string { func app010LibPath(root string) string {
return filepath.Join(root, LibDirName) return filepath.Join(root, LibDirName)
} }
......
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
...@@ -43,8 +44,8 @@ type App001 struct { ...@@ -43,8 +44,8 @@ type App001 struct {
var _ App = (*App001)(nil) var _ App = (*App001)(nil)
// NewApp001 creates an App001 instance. // NewApp001 creates an App001 instance.
func NewApp001(fs afero.Fs, root string) *App001 { func NewApp001(fs afero.Fs, root string, httpClient *http.Client) *App001 {
ba := newBaseApp(fs, root) ba := newBaseApp(fs, root, httpClient)
return &App001{ return &App001{
out: os.Stdout, out: os.Stdout,
...@@ -80,7 +81,7 @@ func (a *App001) AddEnvironment(e *EnvironmentConfig, k8sSpecFlag string, isOver ...@@ -80,7 +81,7 @@ func (a *App001) AddEnvironment(e *EnvironmentConfig, k8sSpecFlag string, isOver
return err return err
} }
_, err = LibUpdater(a.fs, k8sSpecFlag, a.appLibPath(e.Name)) _, err = a.libUpdater.UpdateKSLib(k8sSpecFlag, a.appLibPath(e.Name))
return err return err
} }
...@@ -294,7 +295,7 @@ func (a *App001) convertEnvironment(envName string, dryRun bool) error { ...@@ -294,7 +295,7 @@ func (a *App001) convertEnvironment(envName string, dryRun bool) error {
} }
k8sSpecFlag := fmt.Sprintf("version:%s", env.KubernetesVersion) k8sSpecFlag := fmt.Sprintf("version:%s", env.KubernetesVersion)
_, err = LibUpdater(a.fs, k8sSpecFlag, app010LibPath(a.root)) _, err = a.libUpdater.UpdateKSLib(k8sSpecFlag, app010LibPath(a.root))
if err != nil { if err != nil {
return err return err
} }
......
...@@ -254,17 +254,6 @@ func TestApp001_UpdateTargets(t *testing.T) { ...@@ -254,17 +254,6 @@ func TestApp001_UpdateTargets(t *testing.T) {
} }
func withApp001Fs(t *testing.T, appName string, fn func(app *App001)) { func withApp001Fs(t *testing.T, appName string, fn func(app *App001)) {
ogLibUpdater := LibUpdater
LibUpdater = func(fs afero.Fs, k8sSpecFlag string, libPath string) (string, error) {
path := filepath.Join(libPath, "swagger.json")
stageFile(t, fs, "swagger.json", path)
return "v1.8.7", nil
}
defer func() {
LibUpdater = ogLibUpdater
}()
dir, err := ioutil.TempDir("", "") dir, err := ioutil.TempDir("", "")
require.NoError(t, err) require.NoError(t, err)
...@@ -293,6 +282,12 @@ func withApp001Fs(t *testing.T, appName string, fn func(app *App001)) { ...@@ -293,6 +282,12 @@ func withApp001Fs(t *testing.T, appName string, fn func(app *App001)) {
stageFile(t, fs, appName, "/app.yaml") stageFile(t, fs, appName, "/app.yaml")
app := NewApp001(fs, "/") app := NewApp001(fs, "/", nil)
app.libUpdater = fakeLibUpdater(func(k8sSpecFlag string, libPath string) (string, error) {
path := filepath.Join(libPath, "swagger.json")
stageFile(t, fs, "swagger.json", path)
return "v1.8.7", nil
})
fn(app) fn(app)
} }
...@@ -18,6 +18,7 @@ package app ...@@ -18,6 +18,7 @@ package app
import ( import (
"fmt" "fmt"
"io" "io"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
...@@ -39,9 +40,19 @@ type App010 struct { ...@@ -39,9 +40,19 @@ type App010 struct {
var _ App = (*App010)(nil) var _ App = (*App010)(nil)
// App010Opt is a constructor option for App010
type App010Opt func(*App010)
// App010OptLibUpdater returns an option for setting a KSLibUpdater on an App010
func App010OptLibUpdater(libUpdater KSLibUpdater) App010Opt {
return func(a *App010) {
a.libUpdater = libUpdater
}
}
// NewApp010 creates an App010 instance. // NewApp010 creates an App010 instance.
func NewApp010(fs afero.Fs, root string) *App010 { func NewApp010(fs afero.Fs, root string, httpClient *http.Client, opts ...App010Opt) *App010 {
ba := newBaseApp(fs, root) ba := newBaseApp(fs, root, httpClient)
a := &App010{ a := &App010{
baseApp: ba, baseApp: ba,
...@@ -50,6 +61,10 @@ func NewApp010(fs afero.Fs, root string) *App010 { ...@@ -50,6 +61,10 @@ func NewApp010(fs afero.Fs, root string) *App010 {
libPaths: make(map[string]string), libPaths: make(map[string]string),
} }
for _, optFn := range opts {
optFn(a)
}
return a return a
} }
...@@ -78,7 +93,7 @@ func (a *App010) AddEnvironment(newEnv *EnvironmentConfig, k8sSpecFlag string, i ...@@ -78,7 +93,7 @@ func (a *App010) AddEnvironment(newEnv *EnvironmentConfig, k8sSpecFlag string, i
} }
if k8sSpecFlag != "" { if k8sSpecFlag != "" {
ver, err := LibUpdater(a.fs, k8sSpecFlag, app010LibPath(a.root)) ver, err := a.libUpdater.UpdateKSLib(k8sSpecFlag, app010LibPath(a.root))
if err != nil { if err != nil {
return err return err
} }
...@@ -149,7 +164,7 @@ func (a *App010) LibPath(envName string) (string, error) { ...@@ -149,7 +164,7 @@ func (a *App010) LibPath(envName string) (string, error) {
} }
ver := fmt.Sprintf("version:%s", env.KubernetesVersion) ver := fmt.Sprintf("version:%s", env.KubernetesVersion)
lm, err := lib.NewManager(ver, a.fs, app010LibPath(a.root)) lm, err := lib.NewManager(ver, a.fs, app010LibPath(a.root), a.httpClient)
if err != nil { if err != nil {
return "", err return "", err
} }
......
...@@ -17,6 +17,7 @@ package app ...@@ -17,6 +17,7 @@ package app
import ( import (
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
...@@ -26,6 +27,26 @@ import ( ...@@ -26,6 +27,26 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
type fakeTransport struct {
resp *http.Response
err error
}
func (f *fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return f.resp, f.err
}
// fakeHTTPClient returns an http.Client that will respond with the predefined response and error.
func fakeHTTPClient(resp *http.Response, err error) *http.Client {
c := &http.Client{
Transport: &fakeTransport{
resp: resp,
err: err,
},
}
return c
}
func TestApp010_AddEnvironment(t *testing.T) { func TestApp010_AddEnvironment(t *testing.T) {
withApp010Fs(t, "app010_app.yaml", func(app *App010) { withApp010Fs(t, "app010_app.yaml", func(app *App010) {