8 Commits

Author SHA1 Message Date
88a468a9ce Your commit is writing checks your merge can't cash.
All checks were successful
🏗️✨ Test Build Workflow / 🖥️ 🔨 Build (push) Successful in 25m19s
2025-05-14 16:23:17 -04:00
699878bcb9 Whee, good night. 2025-05-14 16:23:06 -04:00
18a4d90b28 forgot to save that file
Some checks failed
🏗️✨ Test Build Workflow / 🖥️ 🔨 Build (push) Has been cancelled
2025-05-14 16:22:47 -04:00
d3d28947b2 workaround for ant being a pile of fail
Some checks failed
🏗️✨ Test Build Workflow / 🖥️ 🔨 Build (push) Has been cancelled
2025-05-14 16:16:16 -04:00
0ff36d6890 this is my quickfix branch and i will use to do my quickfixes 2025-05-14 15:00:45 -04:00
bcd0c19ead Is there an achievement for this?
Some checks failed
🏗️✨ Test Build Workflow / 🖥️ 🔨 Build (push) Failing after 8m8s
2025-05-14 13:39:20 -04:00
f8f63e418c I really should've committed this when I finished it...
Some checks failed
🏗️✨ Test Build Workflow / 🖥️ 🔨 Build (push) Failing after 4m33s
2025-05-14 13:30:55 -04:00
767e81f8ef Switched off unit test 12 because the build had to go out now and there was no time to fix it properly. 2025-05-14 13:28:34 -04:00
8 changed files with 62 additions and 133 deletions

View File

@@ -33,19 +33,8 @@ jobs:
- name: 🐳 🔨 Build Backend Container - name: 🐳 🔨 Build Backend Container
uses: docker/build-push-action@v6 uses: docker/build-push-action@v6
with: with:
provenance: true platforms: linux/amd64,linux/arm64
sbom: true
push: true push: true
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
tags: siteworxpro/aws-iam-anywhere:${{ gitea.ref_name }} tags: siteworxpro/aws-iam-anywhere:${{ gitea.ref_name }}
- name: 🐳 🔨 Build Backend Container - Latest Tag
uses: docker/build-push-action@v6
with:
provenance: true
sbom: true
push: true
context: .
dockerfile: Dockerfile
tags: siteworxpro/aws-iam-anywhere:latest

View File

@@ -33,7 +33,7 @@ jobs:
- name: 🐳 🔨 Build Backend Container - name: 🐳 🔨 Build Backend Container
uses: docker/build-push-action@v6 uses: docker/build-push-action@v6
with: with:
platforms: linux/amd64 platforms: linux/amd64,linux/arm64
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
tags: siteworxpro/aws-iam-anywhere:${{ gitea.ref_name }} tags: siteworxpro/aws-iam-anywhere:${{ gitea.ref_name }}

View File

@@ -8,17 +8,13 @@ ENV GOPRIVATE=git.siteworxpro.com
RUN go mod download && go build -o aws-iam-anywhere-refresher . RUN go mod download && go build -o aws-iam-anywhere-refresher .
FROM ubuntu:latest AS runtime FROM alpine:latest AS runtime
RUN apt update && apt install -yq ca-certificates curl
RUN curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt \
&& update-ca-certificates
WORKDIR /app WORKDIR /app
COPY --from=build /app/aws-iam-anywhere-refresher /app/aws-iam-anywhere-refresher COPY --from=build /app/aws-iam-anywhere-refresher aws-iam-anywhere-refresher
RUN useradd -b /app iam && \ RUN adduser -D -H iam && \
chown iam:iam /app/aws-iam-anywhere-refresher chown iam:iam /app/aws-iam-anywhere-refresher
USER iam USER iam

View File

@@ -28,7 +28,6 @@ This image runs in a kubernetes cronjob and will create and save new IAM credent
- `TRUSTED_ANCHOR_ARN` ***required*** : the trusted anchor arn - `TRUSTED_ANCHOR_ARN` ***required*** : the trusted anchor arn
- `PRIVATE_KEY` ***required*** : iam private key base64 encoded - `PRIVATE_KEY` ***required*** : iam private key base64 encoded
- `CERTIFICATE` ***required*** : iam certificate base64 encoded - `CERTIFICATE` ***required*** : iam certificate base64 encoded
- `CA_CHAIN` : the certificate chain bundle if needed
```yaml ```yaml

View File

@@ -30,10 +30,13 @@ import (
"errors" "errors"
"fmt" "fmt"
"hash"
"log"
"os"
"strings"
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/scrypt" "golang.org/x/crypto/scrypt"
"hash"
"os"
) )
// as defined in https://datatracker.ietf.org/doc/html/rfc8018#appendix-A.4 // as defined in https://datatracker.ietf.org/doc/html/rfc8018#appendix-A.4
@@ -236,6 +239,9 @@ func readPKCS8PrivateKey(privateKeyId string) (crypto.PrivateKey, error) {
func readPKCS8EncryptedPrivateKey(privateKeyId string, pkcs8Password []byte) (crypto.PrivateKey, error) { func readPKCS8EncryptedPrivateKey(privateKeyId string, pkcs8Password []byte) (crypto.PrivateKey, error) {
block, err := parseDERFromPEMForPKCS8(privateKeyId, encryptedBlockType) block, err := parseDERFromPEMForPKCS8(privateKeyId, encryptedBlockType)
if err != nil { if err != nil {
if Debug && strings.Contains(err.Error(), `The block type detected is PRIVATE KEY`) {
log.Println("PKCS#8 password provided but block type indicates that one isn't required.")
}
return nil, errors.New("could not parse PEM data") return nil, errors.New("could not parse PEM data")
} }

View File

@@ -612,11 +612,14 @@ func encodeDer(der []byte) (string, error) {
} }
func parseDERFromPEM(pemDataId string, blockType string) (*pem.Block, error) { func parseDERFromPEM(pemDataId string, blockType string) (*pem.Block, error) {
b := []byte(pemDataId) bts, err := os.ReadFile(pemDataId)
if err != nil {
return nil, err
}
var block *pem.Block var block *pem.Block
for len(b) > 0 { for len(bts) > 0 {
block, b = pem.Decode(b) block, bts = pem.Decode(bts)
if block == nil { if block == nil {
return nil, errors.New("unable to parse PEM data") return nil, errors.New("unable to parse PEM data")
} }
@@ -628,18 +631,25 @@ func parseDERFromPEM(pemDataId string, blockType string) (*pem.Block, error) {
} }
func ReadCertificateBundleData(certificateBundleId string) ([]*x509.Certificate, error) { func ReadCertificateBundleData(certificateBundleId string) ([]*x509.Certificate, error) {
bts, err := os.ReadFile(certificateBundleId)
if err != nil {
return nil, err
}
var derBytes []byte var derBytes []byte
var block *pem.Block var block *pem.Block
block, _ = pem.Decode([]byte(certificateBundleId)) for len(bts) > 0 {
block, bts = pem.Decode(bts)
if block.Type != "CERTIFICATE" { if block == nil {
return nil, errors.New("invalid certificate chain") break
}
if block.Type != "CERTIFICATE" {
return nil, errors.New("invalid certificate chain")
}
blockBytes := block.Bytes
derBytes = append(derBytes, blockBytes...)
} }
blockBytes := block.Bytes
derBytes = append(derBytes, blockBytes...)
return x509.ParseCertificates(derBytes) return x509.ParseCertificates(derBytes)
} }

View File

@@ -1,11 +1,6 @@
package config package config
import ( import "git.siteworxpro.com/packages/go/utilities/Env"
"encoding/base64"
"fmt"
"git.siteworxpro.com/packages/go/utilities/Env"
"regexp"
)
const ( const (
namespace Env.EnvironmentVariable = "NAMESPACE" namespace Env.EnvironmentVariable = "NAMESPACE"
@@ -15,10 +10,8 @@ const (
trustedAnchorArn Env.EnvironmentVariable = "TRUSTED_ANCHOR_ARN" trustedAnchorArn Env.EnvironmentVariable = "TRUSTED_ANCHOR_ARN"
privateKey Env.EnvironmentVariable = "PRIVATE_KEY" privateKey Env.EnvironmentVariable = "PRIVATE_KEY"
certificate Env.EnvironmentVariable = "CERTIFICATE" certificate Env.EnvironmentVariable = "CERTIFICATE"
bundleId Env.EnvironmentVariable = "CA_CHAIN"
sessionDuration Env.EnvironmentVariable = "SESSION_DURATION" sessionDuration Env.EnvironmentVariable = "SESSION_DURATION"
restartDeployments Env.EnvironmentVariable = "RESTART_DEPLOYMENTS" restartDeployments Env.EnvironmentVariable = "RESTART_DEPLOYMENTS"
fetchOnly Env.EnvironmentVariable = "FETCH_ONLY"
) )
type Config struct{} type Config struct{}
@@ -27,59 +20,6 @@ func NewConfig() *Config {
return &Config{} return &Config{}
} }
func (c Config) Valid() error {
// Certificate Required
if c.Certificate() == "" {
return fmt.Errorf("certificate is required")
}
// Private Key Required
if c.PrivateKey() == "" {
return fmt.Errorf("private Key is required")
}
// Role ARN Required
if c.RoleArn() == "" {
return fmt.Errorf("role ARN is required")
}
if !regexp.MustCompile(`^arn:aws:iam::[0-9]{10,13}:role/[\w\D]*$`).MatchString(c.RoleArn()) {
return fmt.Errorf("role ARN %s is invalid", c.RoleArn())
}
if c.ProfileArn() == "" {
return fmt.Errorf("profile ARN is required")
}
if !regexp.MustCompile(`^arn:aws:rolesanywhere:[\w-]*:\d{10,12}:profile/[\w\D]*$`).MatchString(c.ProfileArn()) {
return fmt.Errorf("profile ARN %s is invalid", c.ProfileArn())
}
// Trusted Anchor ARN Required
if c.TrustedAnchor() == "" {
return fmt.Errorf("trusted anchor ARN is required")
}
if !regexp.MustCompile(`^arn:aws:rolesanywhere:[\w-]*:\d{10,12}:trust-anchor/[\w\D]*$`).MatchString(c.TrustedAnchor()) {
return fmt.Errorf("trusted anchor %s ARN is invalid", c.TrustedAnchor())
}
return nil
}
func (Config) BundleId() string {
v, err := base64.StdEncoding.DecodeString(bundleId.GetEnvString(""))
if err != nil {
return ""
}
return string(v)
}
func (Config) FetchOnly() bool {
return fetchOnly.GetEnvBool(false)
}
func (Config) Namespace() string { func (Config) Namespace() string {
return namespace.GetEnvString("") return namespace.GetEnvString("")
} }
@@ -101,21 +41,11 @@ func (Config) TrustedAnchor() string {
} }
func (Config) PrivateKey() string { func (Config) PrivateKey() string {
v, err := base64.StdEncoding.DecodeString(privateKey.GetEnvString("")) return privateKey.GetEnvString("")
if err != nil {
return ""
}
return string(v)
} }
func (Config) Certificate() string { func (Config) Certificate() string {
v, err := base64.StdEncoding.DecodeString(certificate.GetEnvString("")) return certificate.GetEnvString("")
if err != nil {
return ""
}
return string(v)
} }
func (Config) SessionDuration() int64 { func (Config) SessionDuration() int64 {

51
main.go
View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/base64"
helper "gitea.siteworxpro.com/Siteworxpro/aws-iam-anywhere-refresher/aws_signing_helper" helper "gitea.siteworxpro.com/Siteworxpro/aws-iam-anywhere-refresher/aws_signing_helper"
"gitea.siteworxpro.com/Siteworxpro/aws-iam-anywhere-refresher/cmd" "gitea.siteworxpro.com/Siteworxpro/aws-iam-anywhere-refresher/cmd"
appConfig "gitea.siteworxpro.com/Siteworxpro/aws-iam-anywhere-refresher/config" appConfig "gitea.siteworxpro.com/Siteworxpro/aws-iam-anywhere-refresher/config"
@@ -17,25 +18,39 @@ func main() {
ReportTimestamp: true, ReportTimestamp: true,
TimeFormat: time.RFC3339, TimeFormat: time.RFC3339,
}) })
l.Info("Starting credentials refresh") l.Info("Starting credentials refresh")
client, err := kube_client.NewKubeClient()
if err != nil {
l.Error("Failed to create kubernetes client", "error", err)
os.Exit(1)
}
c := appConfig.NewConfig() c := appConfig.NewConfig()
err := c.Valid() privateKey, err := base64.StdEncoding.DecodeString(c.PrivateKey())
if err != nil { if err != nil {
l.Error("Invalid configuration", "error", err) l.Error("Failed to decode private key", "error", err)
os.Exit(1)
}
certificate, err := base64.StdEncoding.DecodeString(c.Certificate())
if err != nil {
l.Error("Failed to decode certificate", "error", err)
os.Exit(1) os.Exit(1)
} }
credentials, err := cmd.Run(&helper.CredentialsOpts{ credentials, err := cmd.Run(&helper.CredentialsOpts{
PrivateKeyId: c.PrivateKey(), PrivateKeyId: string(privateKey),
CertificateId: c.Certificate(), CertificateId: string(certificate),
CertificateBundleId: c.BundleId(), CertIdentifier: helper.CertIdentifier{
RoleArn: c.RoleArn(), SystemStoreName: "MY",
ProfileArnStr: c.ProfileArn(), },
TrustAnchorArnStr: c.TrustedAnchor(), RoleArn: c.RoleArn(),
SessionDuration: int(c.SessionDuration()), ProfileArnStr: c.ProfileArn(),
TrustAnchorArnStr: c.TrustedAnchor(),
SessionDuration: int(c.SessionDuration()),
}) })
if err != nil { if err != nil {
@@ -46,22 +61,6 @@ func main() {
l.Info("Credentials refreshed") l.Info("Credentials refreshed")
if c.FetchOnly() {
l.Info("Fetch only mode, skipping secret update")
l.Info("AccessKeyId", "access-key-id", credentials.AccessKeyId)
l.Info("SecretAccessKey", "secret-access-key", credentials.SecretAccessKey)
l.Info("SessionToken", "session-token", credentials.SessionToken)
os.Exit(0)
}
client, err := kube_client.NewKubeClient()
if err != nil {
l.Error("Failed to create kubernetes client", "error", err)
os.Exit(1)
}
_, err = client.GetSecret(c.Namespace(), c.Secret()) _, err = client.GetSecret(c.Namespace(), c.Secret())
if err != nil { if err != nil {
l.Error("Failed to get secret", "error", err) l.Error("Failed to get secret", "error", err)