Skip to content

Commit

Permalink
Added password parameter on it-chain ivm deploy command & add test code.
Browse files Browse the repository at this point in the history
  • Loading branch information
nesticat authored and junbeomlee committed Sep 1, 2018
1 parent bc9d22a commit 29659e9
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 20 deletions.
14 changes: 8 additions & 6 deletions cmd/ivm/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,31 @@ import (
func DeployCmd() cli.Command {
return cli.Command{
Name: "deploy",
Usage: "it-chain ivm deploy [icode git url] [ssh path]",
Usage: "it-chain ivm deploy [icode git url] [ssh path] [password]",
Action: func(c *cli.Context) error {

gitUrl := c.Args().Get(0)
sshPath := c.Args().Get(1)
deploy(gitUrl, sshPath)
password := c.Args().Get(2)
deploy(gitUrl, sshPath, password)

return nil
},
}
}

func deploy(gitUrl string, sshPath string) {
func deploy(gitUrl string, sshPath string, password string) {

config := conf.GetConfiguration()
client := rpc.NewClient(config.Engine.Amqp)

defer client.Close()

deployCommand := command.Deploy{
ICodeId: xid.New().String(),
Url: gitUrl,
SshPath: sshPath,
ICodeId: xid.New().String(),
Url: gitUrl,
SshPath: sshPath,
Password: password,
}

log.Printf("[Cmd] deploying icode...")
Expand Down
7 changes: 4 additions & 3 deletions common/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ type ExecuteICode struct {
}

type Deploy struct {
ICodeId string
Url string
SshPath string
ICodeId string
Url string
SshPath string
Password string
}
type UnDeploy struct {
ICodeId string
Expand Down
4 changes: 2 additions & 2 deletions ivm/api/icode_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func NewICodeApi(containerService ivm.ContainerService, gitService ivm.GitServic
}
}

func (i ICodeApi) Deploy(id string, baseSaveUrl string, gitUrl string, sshPath string) (ivm.ICode, error) {
func (i ICodeApi) Deploy(id string, baseSaveUrl string, gitUrl string, sshPath string, password string) (ivm.ICode, error) {
logger.Info(nil, fmt.Sprintf("[IVM] Deploying icode - url: [%s]", gitUrl))

// clone icode. in clone function, metaCreatedEvent will publish
icode, err := i.GitService.Clone(id, baseSaveUrl, gitUrl, sshPath)
icode, err := i.GitService.Clone(id, baseSaveUrl, gitUrl, sshPath, password)

if err != nil {
return ivm.ICode{}, err
Expand Down
8 changes: 4 additions & 4 deletions ivm/api/icode_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestICodeApi_Deploy(t *testing.T) {

api, containerService := setUp(t)

icode, err := api.Deploy("1", savePath, "github.com/junbeomlee/learn-icode", sshPath)
icode, err := api.Deploy("1", savePath, "github.com/junbeomlee/learn-icode", sshPath, "")
defer api.UnDeploy("1")

assert.NoError(t, err)
Expand All @@ -65,7 +65,7 @@ func TestICodeApi_UnDeploy(t *testing.T) {
defer tearDown1()

api, containerService := setUp(t)
_, err = api.Deploy("1", savePath, "github.com/junbeomlee/learn-icode", sshPath)
_, err = api.Deploy("1", savePath, "github.com/junbeomlee/learn-icode", sshPath, "")
assert.NoError(t, err)
assert.Equal(t, containerService.GetRunningICodeIDList()[0], "1")

Expand All @@ -84,7 +84,7 @@ func TestICodeApi_ExecuteRequest(t *testing.T) {
defer tearDown1()

api, _ := setUp(t)
_, err = api.Deploy("1", savePath, "github.com/junbeomlee/learn-icode", sshPath)
_, err = api.Deploy("1", savePath, "github.com/junbeomlee/learn-icode", sshPath, "")
defer api.UnDeploy("1")

result, err := api.ExecuteRequest(ivm.Request{
Expand All @@ -108,7 +108,7 @@ func TestICodeApi_ExecuteRequestList(t *testing.T) {
defer tearDown1()

api, _ := setUp(t)
_, err = api.Deploy("1", savePath, "github.com/junbeomlee/learn-icode", sshPath)
_, err = api.Deploy("1", savePath, "github.com/junbeomlee/learn-icode", sshPath, "")
defer api.UnDeploy("1")

results := api.ExecuteRequestList([]ivm.Request{
Expand Down
2 changes: 1 addition & 1 deletion ivm/infra/adapter/deploy_command_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewDeployCommandHandler(icodeApi api.ICodeApi) *DeployCommandHandler {
func (d *DeployCommandHandler) HandleDeployCommand(deployCommand command.Deploy) (ivm.ICode, rpc.Error) {

savePath := os.Getenv("GOPATH") + "/src/github.com/it-chain/engine/.tmp/"
icode, err := d.icodeApi.Deploy(deployCommand.ICodeId, savePath, deployCommand.Url, deployCommand.SshPath)
icode, err := d.icodeApi.Deploy(deployCommand.ICodeId, savePath, deployCommand.Url, deployCommand.SshPath, deployCommand.Password)

if err != nil {
logger.Error(nil, fmt.Sprintf("[Icode] fail to deploy ivm, url %s", deployCommand.Url))
Expand Down
4 changes: 2 additions & 2 deletions ivm/infra/git/icode_git_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewRepositoryService() *RepositoryService {
return &RepositoryService{}
}

func (gApi *RepositoryService) Clone(id string, baseSavePath string, repositoryUrl string, sshPath string) (ivm.ICode, error) {
func (gApi *RepositoryService) Clone(id string, baseSavePath string, repositoryUrl string, sshPath string, password string) (ivm.ICode, error) {
logger.Info(nil, fmt.Sprintf("[IVM] Cloning Icode - url: [%s]", repositoryUrl))

giturl, err := toSshUrl(repositoryUrl)
Expand All @@ -65,7 +65,7 @@ func (gApi *RepositoryService) Clone(id string, baseSavePath string, repositoryU
}
}

sshAuth, err := ssh.NewPublicKeysFromFile("git", sshPath, "")
sshAuth, err := ssh.NewPublicKeysFromFile("git", sshPath, password)
if err != nil {
return ivm.ICode{}, err
}
Expand Down
83 changes: 82 additions & 1 deletion ivm/infra/git/icode_git_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,59 @@ func TestICodeGitStoreApi_Clone(t *testing.T) {

for _, test := range tests {
//when
meta, err := icodeApi.Clone(test.ID, baseTempPath, test.InputGitURL, sshPath)
meta, err := icodeApi.Clone(test.ID, baseTempPath, test.InputGitURL, sshPath, "")

if err == nil {
// ivm ID 는 랜덤이기때문에 실데이터에서 주입
// commit hash 는 repo 상황에따라 바뀌기 때문에 주입
test.OutputMeta.ID = meta.ID
test.OutputMeta.CommitHash = meta.CommitHash
}

//then
assert.Equal(t, test.OutputMeta, meta)
assert.Equal(t, test.OutputErr, err)
}
}

func TestICodeGitStoreApi_CloneWithPassword(t *testing.T) {
baseTempPath := "./.tmp"
sshPathWithPassword := "./id_rsa_pwd"
err, teardown := generatePriKeyWithPassword(sshPathWithPassword)
assert.NoError(t, err, "err in generate pri key file")
defer teardown()
os.RemoveAll(baseTempPath)
defer os.RemoveAll(baseTempPath)

//given
tests := map[string]struct {
ID string
InputGitURL string
InputPwd string
OutputMeta ivm.ICode
OutputErr error
}{
"success": {
ID: "1",
InputGitURL: "github.com/nesticat/test_icode",
InputPwd: "pwdtest",
OutputMeta: ivm.ICode{RepositoryName: "test_icode", GitUrl: "github.com/nesticat/test_icode", Path: baseTempPath + "/" + "test_icode"},
OutputErr: nil,
},
"fail": {
ID: "1",
InputGitURL: "github.com/nesticat/test_icode",
InputPwd: "wrongpassword",
OutputMeta: ivm.ICode{},
OutputErr: errors.New("x509: decryption password incorrect"),
},
}

icodeApi := git.NewRepositoryService()

for _, test := range tests {
//when
meta, err := icodeApi.Clone(test.ID, baseTempPath, test.InputGitURL, sshPathWithPassword, test.InputPwd)

if err == nil {
// ivm ID 는 랜덤이기때문에 실데이터에서 주입
Expand Down Expand Up @@ -106,3 +158,32 @@ func generatePriKey(path string) (error, func() error) {
return nil
}
}

func generatePriKeyWithPassword(path string) (error, func() error) {
_, err := os.Stat(path)
if err == nil {
os.Remove(path)
}

file, err := os.Create(path)
if err != nil {
return err, nil
}

src := "2d2d2d2d2d424547494e205253412050524956415445204b45592d2d2d2d2d0a50726f632d547970653a20342c454e435259505445440a44454b2d496e666f3a204145532d3132382d4342432c36383741323545374230353641444431343139343343334643373831453638380a0a4b666e3757374d6f5777737a494c5a4d5552476b734448742b46414751445a324e3069734530626e73537245544a5a66303650715a367a4a5878353477324f510a5744354541715253366776336d4c4564364c37336243586a69484b6875304933506b52626f2f35476d4a5432572f2b4867764d6c4945796533796b74677430650a70385078356769652b7765742b4f76524c79493958374145685465745a6c35683871474652516c38596361495546345776623854384143596a543552456554680a62505a324968513672396f467063354b627a304362785841326c3931316537615055504c506165766f49573377384d7274676d79776e554d62426e45345a32310a4457477444745447334c4b35534141747930615445353678666d6a33566649463972524e5459534761783947754e54502f6f475847514e63706e7266334875470a64574335766d517166567a42544e32744847545a57322b494a516b4b34766f446d6b69736f62385434563049416f46683272764544684b51732b6374546e56630a646b634e5537304e6b5869573562534e53676179416553347062425379377248574774423739335853456251586e31556f7037384e4b4755686a795137542b460a635544747066417577735569365833644d7669776c5769322b78493073595475507553376230646566584c44417a78634e72496d4a7232704d503579363169340a474c69536238417361586268553778363134476e5844316b2b397a6f434755504a41742f796436367957616c4f74535176447a57476d71756855714877486a450a6e703469552f33585476344452744c6169715955534a4456616835544641536464797a38774c6e52476f5a4b37476c347555586468456b696f5a5576793641320a6c434b555a4441396c376c796a78385534664c7a636f6d636f2f78464f5830304936784b4579353650382b71502b35454f7648384a4d79394f462b4c433837660a6d764158373031432b306446794d3532357243504b446e776e34626e654277796848663754542b614862466934666a70707650504a5a683770446571454c6e4d0a6e2b4330663050776b656949546330643373527a6645374b4b786f514232474a2b2f754565552f45612f71726564562f674b45584f4b366152357855522f6a450a545342364e656b577746773669747561536c666e47593949466738574e3942357079336e3558347034746f5a4e4e737231674c6d43326d794a5a6d563470457a0a4d665275704f433268395a4d3243372b4b72626a65473448647679737765625331484263594c71354f4d77506173516f4e7a54365454684d3764724242762f710a7631594c2f385355664e37736e4d79547272394346396a6b6c7a676c6e3242346a7a68766b2b76564c46337a6437794b755748614e553836305166416a4e35330a355a4175356930593279694e756b503978775938484841673461746c38703576594f667672516a633470515766304d744f344a386a37783034797a6e586138530a62664f783730306959367a572f6f4433363679766d6870764369494a7369384b56546b654f4d433761626a543878505258703656526361776e667247766466440a306b554e67774c72544c6b4b76426e796937577a3130646a37323850653431334f503161396d7a56796277397037304c6b7a50426948726f32352f5550486a7a0a61717159774f715a33352f553073573570517736584872776533434235334378436e416d7168734e56757939644575485a57465167306e31696132714c42504c0a4b6c384157452f49796e692f4d4c54376870726a446a39796a6676414d555833577a613736435570426f506943624e67504a32785338556b676e6a46476b71410a4771383168367a7647326f5a4661417645345470557a36454b77504269386842373147584d575771504a675637524253467a5062526a5577373650414f5657450a52394f4f39766b59776231365742394a4a586a586177766f6958484b4e6836677366344e4f764e6c6d494d2f6d655364385a53666167575573384c59345a78590a70574a554e4973795a792b2f346d7333316b7033453948436a636c78356e4352315a6742314f32505438454362584867714936416251774f79385857315436680a4675683949424a7339783959505776336c5937506655564951732f63557a514d3071666e5363565a54433835694f45554177623958766c6b4646346567774b720a2d2d2d2d2d454e44205253412050524956415445204b45592d2d2d2d2d0a"
p, _ := hex.DecodeString(src)

file.WriteString(fmt.Sprintf("%s", p))
return nil, func() error {

if err := os.RemoveAll(path); err != nil {
return err
}

if err := file.Close(); err != nil {
return err
}

return nil
}
}
2 changes: 1 addition & 1 deletion ivm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ContainerService interface {

type GitService interface {
//clone code from deploy info
Clone(id string, baseSavePath string, repositoryUrl string, sshPath string) (ICode, error)
Clone(id string, baseSavePath string, repositoryUrl string, sshPath string, password string) (ICode, error)
}

type EventService interface {
Expand Down

0 comments on commit 29659e9

Please sign in to comment.