Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
fd4e0e9d
Commit
fd4e0e9d
authored
Jun 13, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/amazon: StepGetPassword
parent
11402709
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
165 additions
and
0 deletions
+165
-0
builder/amazon/common/run_config.go
builder/amazon/common/run_config.go
+6
-0
builder/amazon/common/step_get_password.go
builder/amazon/common/step_get_password.go
+155
-0
builder/amazon/ebs/builder.go
builder/amazon/ebs/builder.go
+4
-0
No files found.
builder/amazon/common/run_config.go
View file @
fd4e0e9d
...
...
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"time"
"github.com/mitchellh/packer/common/uuid"
"github.com/mitchellh/packer/helper/communicator"
...
...
@@ -27,6 +28,7 @@ type RunConfig struct {
TemporaryKeyPairName
string
`mapstructure:"temporary_key_pair_name"`
UserData
string
`mapstructure:"user_data"`
UserDataFile
string
`mapstructure:"user_data_file"`
WindowsPasswordTimeout
time
.
Duration
`mapstructure:"windows_password_timeout"`
VpcId
string
`mapstructure:"vpc_id"`
// Communicator settings
...
...
@@ -40,6 +42,10 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
"packer %s"
,
uuid
.
TimeOrderedUUID
())
}
if
c
.
WindowsPasswordTimeout
==
0
{
c
.
WindowsPasswordTimeout
=
10
*
time
.
Minute
}
// Validation
errs
:=
c
.
Comm
.
Prepare
(
ctx
)
if
c
.
SourceAmi
==
""
{
...
...
builder/amazon/common/step_get_password.go
0 → 100644
View file @
fd4e0e9d
package
common
import
(
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/helper/communicator"
"github.com/mitchellh/packer/packer"
)
// StepGetPassword reads the password from a Windows server and sets it
// on the WinRM config.
type
StepGetPassword
struct
{
Comm
*
communicator
.
Config
Timeout
time
.
Duration
}
func
(
s
*
StepGetPassword
)
Run
(
state
multistep
.
StateBag
)
multistep
.
StepAction
{
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
image
:=
state
.
Get
(
"source_image"
)
.
(
*
ec2
.
Image
)
// Skip if we're not Windows...
if
*
image
.
Platform
!=
"windows"
{
log
.
Printf
(
"[INFO] Not Windows, skipping get password..."
)
return
multistep
.
ActionContinue
}
// If we already have a password, skip it
if
s
.
Comm
.
WinRMPassword
!=
""
{
ui
.
Say
(
"Skipping waiting for password since WinRM password set..."
)
return
multistep
.
ActionContinue
}
// Get the password
var
password
string
var
err
error
cancel
:=
make
(
chan
struct
{})
waitDone
:=
make
(
chan
bool
,
1
)
go
func
()
{
ui
.
Say
(
"Waiting for auto-generated password for instance..."
)
password
,
err
=
s
.
waitForPassword
(
state
,
cancel
)
waitDone
<-
true
}()
timeout
:=
time
.
After
(
s
.
Timeout
)
WaitLoop
:
for
{
// Wait for either SSH to become available, a timeout to occur,
// or an interrupt to come through.
select
{
case
<-
waitDone
:
if
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Error waiting for password: %s"
,
err
))
state
.
Put
(
"error"
,
err
)
return
multistep
.
ActionHalt
}
ui
.
Message
(
"Password retrieved!"
)
s
.
Comm
.
WinRMPassword
=
password
break
WaitLoop
case
<-
timeout
:
err
:=
fmt
.
Errorf
(
"Timeout waiting for password."
)
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
close
(
cancel
)
return
multistep
.
ActionHalt
case
<-
time
.
After
(
1
*
time
.
Second
)
:
if
_
,
ok
:=
state
.
GetOk
(
multistep
.
StateCancelled
);
ok
{
// The step sequence was cancelled, so cancel waiting for password
// and just start the halting process.
close
(
cancel
)
log
.
Println
(
"[WARN] Interrupt detected, quitting waiting for password."
)
return
multistep
.
ActionHalt
}
}
}
return
multistep
.
ActionContinue
}
func
(
s
*
StepGetPassword
)
Cleanup
(
multistep
.
StateBag
)
{}
func
(
s
*
StepGetPassword
)
waitForPassword
(
state
multistep
.
StateBag
,
cancel
<-
chan
struct
{})
(
string
,
error
)
{
ec2conn
:=
state
.
Get
(
"ec2"
)
.
(
*
ec2
.
EC2
)
instance
:=
state
.
Get
(
"instance"
)
.
(
*
ec2
.
Instance
)
privateKey
:=
state
.
Get
(
"privateKey"
)
.
(
string
)
for
{
select
{
case
<-
cancel
:
log
.
Println
(
"[INFO] Retrieve password wait cancelled. Exiting loop."
)
return
""
,
errors
.
New
(
"Retrieve password wait cancelled"
)
case
<-
time
.
After
(
5
*
time
.
Second
)
:
}
resp
,
err
:=
ec2conn
.
GetPasswordData
(
&
ec2
.
GetPasswordDataInput
{
InstanceID
:
instance
.
InstanceID
,
})
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error retrieving auto-generated instance password: %s"
,
err
)
return
""
,
err
}
if
resp
.
PasswordData
!=
nil
&&
*
resp
.
PasswordData
!=
""
{
decryptedPassword
,
err
:=
decryptPasswordDataWithPrivateKey
(
*
resp
.
PasswordData
,
[]
byte
(
privateKey
))
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error decrypting auto-generated instance password: %s"
,
err
)
return
""
,
err
}
return
decryptedPassword
,
nil
}
}
}
func
decryptPasswordDataWithPrivateKey
(
passwordData
string
,
pemBytes
[]
byte
)
(
string
,
error
)
{
encryptedPasswd
,
err
:=
base64
.
StdEncoding
.
DecodeString
(
passwordData
)
if
err
!=
nil
{
return
""
,
err
}
block
,
_
:=
pem
.
Decode
(
pemBytes
)
var
asn1Bytes
[]
byte
if
_
,
ok
:=
block
.
Headers
[
"DEK-Info"
];
ok
{
return
""
,
errors
.
New
(
"encrypted private key isn't yet supported"
)
/*
asn1Bytes, err = x509.DecryptPEMBlock(block, password)
if err != nil {
return "", err
}
*/
}
else
{
asn1Bytes
=
block
.
Bytes
}
key
,
err
:=
x509
.
ParsePKCS1PrivateKey
(
asn1Bytes
)
if
err
!=
nil
{
return
""
,
err
}
out
,
err
:=
rsa
.
DecryptPKCS1v15
(
nil
,
key
,
encryptedPasswd
)
if
err
!=
nil
{
return
""
,
err
}
return
string
(
out
),
nil
}
builder/amazon/ebs/builder.go
View file @
fd4e0e9d
...
...
@@ -113,6 +113,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
BlockDevices
:
b
.
config
.
BlockDevices
,
Tags
:
b
.
config
.
RunTags
,
},
&
awscommon
.
StepGetPassword
{
Comm
:
&
b
.
config
.
RunConfig
.
Comm
,
Timeout
:
b
.
config
.
WindowsPasswordTimeout
,
},
&
communicator
.
StepConnect
{
Config
:
&
b
.
config
.
RunConfig
.
Comm
,
Host
:
awscommon
.
SSHHost
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment