2 files changed, 11 insertions(+), 12 deletions(-)
M main.go
M s3.go
M main.go => main.go +2 -6
@@ 7,18 7,14 @@ import (
// GetCertificate will return a certificate signer from a private key in a S3 Bucket
func GetCertificate(credentials Credentials, file File) (ssh.Signer, error) {
s3Service := connectToS3Service(credentials)
- body, err := getContentFromS3(s3Service, file)
- if err != nil {
- return nil, err
- }
- content, err := getContentBytes(body)
+ body, err := getContentFromS3(s3Service, file)
if err != nil {
return nil, err
}
// Create the Signer for this private key
- signer, err := ssh.ParsePrivateKey(content)
+ signer, err := ssh.ParsePrivateKey(body)
if err != nil {
return nil, err
}
M s3.go => s3.go +9 -6
@@ 2,7 2,7 @@ package certificateS3
import (
"context"
- "io"
+ "errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
@@ 26,7 26,7 @@ func connectToS3Service(creds Credentials) *s3.S3 {
})
}
-func getContentFromS3(s3Service *s3.S3, file File) (io.ReadCloser, error) {
+func getContentFromS3(s3Service *s3.S3, file File) ([]byte, error) {
ctx := context.Background()
result, err := s3Service.GetObjectWithContext(ctx, &s3.GetObjectInput{
@@ 38,14 38,17 @@ func getContentFromS3(s3Service *s3.S3, file File) (io.ReadCloser, error) {
// Cast err to awserr.Error to handle specific error codes.
aerr, ok := err.(awserr.Error)
if ok && aerr.Code() == s3.ErrCodeNoSuchKey {
- // Specific error code handling
+ return nil, errors.New("Certificate does not exists.")
}
return nil, err
}
- // Make sure to close the body when done with it for S3 GetObject APIs or
- // will leak connections.
defer result.Body.Close()
- return result.Body, nil
+ contentBytes, err := getContentBytes(result.Body)
+ if err != nil {
+ return nil, err
+ }
+
+ return contentBytes, nil
}