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
6507afd3
Commit
6507afd3
authored
Dec 12, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/googlecompute: private key tests
parent
3fad2421
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
1 deletion
+85
-1
builder/googlecompute/private_key.go
builder/googlecompute/private_key.go
+7
-1
builder/googlecompute/private_key_test.go
builder/googlecompute/private_key_test.go
+78
-0
No files found.
builder/googlecompute/private_key.go
View file @
6507afd3
...
...
@@ -8,29 +8,35 @@ import (
"io/ioutil"
)
// processPrivateKeyFile.
// processPrivateKeyFile takes a private key file and an optional passphrase
// and decodes it to a byte slice.
func
processPrivateKeyFile
(
privateKeyFile
,
passphrase
string
)
([]
byte
,
error
)
{
rawPrivateKeyBytes
,
err
:=
ioutil
.
ReadFile
(
privateKeyFile
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Failed loading private key file: %s"
,
err
)
}
PEMBlock
,
_
:=
pem
.
Decode
(
rawPrivateKeyBytes
)
if
PEMBlock
==
nil
{
return
nil
,
fmt
.
Errorf
(
"%s does not contain a vaild private key"
,
privateKeyFile
)
}
if
x509
.
IsEncryptedPEMBlock
(
PEMBlock
)
{
if
passphrase
==
""
{
return
nil
,
errors
.
New
(
"a passphrase must be specified when using an encrypted private key"
)
}
decryptedPrivateKeyBytes
,
err
:=
x509
.
DecryptPEMBlock
(
PEMBlock
,
[]
byte
(
passphrase
))
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Failed decrypting private key: %s"
,
err
)
}
b
:=
&
pem
.
Block
{
Type
:
"RSA PRIVATE KEY"
,
Bytes
:
decryptedPrivateKeyBytes
,
}
return
pem
.
EncodeToMemory
(
b
),
nil
}
return
rawPrivateKeyBytes
,
nil
}
builder/googlecompute/private_key_test.go
0 → 100644
View file @
6507afd3
package
googlecompute
import
(
"crypto/rand"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"os"
"testing"
)
func
testPrivateKeyFile
(
t
*
testing
.
T
)
string
{
tf
,
err
:=
ioutil
.
TempFile
(
""
,
"packer"
)
if
err
!=
nil
{
t
.
Fatalf
(
"bad: %s"
,
err
)
}
defer
tf
.
Close
()
b
:=
&
pem
.
Block
{
Type
:
"RSA PRIVATE KEY"
,
Bytes
:
[]
byte
(
"what"
),
}
if
err
:=
pem
.
Encode
(
tf
,
b
);
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
return
tf
.
Name
()
}
func
TestProcesssPrivateKeyFile
(
t
*
testing
.
T
)
{
path
:=
testPrivateKeyFile
(
t
)
defer
os
.
Remove
(
path
)
data
,
err
:=
processPrivateKeyFile
(
path
,
""
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
len
(
data
)
<=
0
{
t
.
Fatalf
(
"bad: %#v"
,
data
)
}
}
func
TestProcessPrivateKeyFile_encrypted
(
t
*
testing
.
T
)
{
// Encrypt the file
b
,
err
:=
x509
.
EncryptPEMBlock
(
rand
.
Reader
,
"RSA PRIVATE KEY"
,
[]
byte
(
"what"
),
[]
byte
(
"password"
),
x509
.
PEMCipherAES128
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
tf
,
err
:=
ioutil
.
TempFile
(
""
,
"packer"
)
if
err
!=
nil
{
t
.
Fatalf
(
"bad: %s"
,
err
)
}
defer
os
.
Remove
(
tf
.
Name
())
err
=
pem
.
Encode
(
tf
,
b
)
tf
.
Close
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
path
:=
tf
.
Name
()
// Should have an error with a bad password
if
_
,
err
:=
processPrivateKeyFile
(
path
,
"bad"
);
err
==
nil
{
t
.
Fatal
(
"should error"
)
}
if
_
,
err
:=
processPrivateKeyFile
(
path
,
"password"
);
err
!=
nil
{
t
.
Fatalf
(
"bad: %s"
,
err
)
}
}
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