Skip to content

Commit

Permalink
docs: updated to be a bit more fun
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywolf132 committed Nov 26, 2024
1 parent 8ec1c35 commit 0883536
Show file tree
Hide file tree
Showing 4 changed files with 493 additions and 149 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.19', '1.20', '1.21']

steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
go-version: '1.23'

- name: Get dependencies
run: go mod download
Expand All @@ -31,7 +28,7 @@ jobs:
- name: Upload coverage
uses: actions/upload-artifact@v3
with:
name: coverage-${{ matrix.go-version }}
name: coverage
path: coverage.out

build:
Expand All @@ -40,7 +37,6 @@ jobs:
needs: test
strategy:
matrix:
go-version: ['1.21']
platform: [ubuntu-latest, macos-latest, windows-latest]

steps:
Expand All @@ -49,7 +45,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
go-version: '1.23'

- name: Get dependencies
run: go mod download
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
go-version: '1.23'

- name: Get dependencies
run: go mod download
Expand All @@ -39,7 +39,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
go-version: '1.23'

- name: Get dependencies
run: go mod download
Expand Down
226 changes: 86 additions & 140 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,91 @@
# SecretFetch
# 🔐 SecretFetch

SecretFetch is a powerful and easy-to-use Go library for managing secrets in your applications. It provides a seamless way to fetch secrets from AWS Secrets Manager, environment variables, or fallback values, with built-in validation, transformation, and caching capabilities.
> Your secrets deserve better than hardcoding. SecretFetch makes secret management a breeze!
## Features
[![CI](https://github.com/crazywolf132/SecretFetch/actions/workflows/ci.yml/badge.svg)](https://github.com/crazywolf132/SecretFetch/actions/workflows/ci.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/crazywolf132/SecretFetch)](https://goreportcard.com/report/github.com/crazywolf132/SecretFetch)
[![GoDoc](https://godoc.org/github.com/crazywolf132/SecretFetch?status.svg)](https://godoc.org/github.com/crazywolf132/SecretFetch)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

- AWS Secrets Manager integration (AWS SDK v2)
- Environment variable support with optional prefixing
- Fallback values for development and testing
- Native Go type support with automatic type conversion
- Simple, intuitive API using struct tags
- Safe secret handling with masking in logs
- Built-in validation with custom validators
- Pattern matching with regex support
- JSON/YAML parsing for complex configurations
- Base64 decoding support
- Configurable caching with TTL
- Value transformers for custom processing
- Concurrent access support
- Flexible configuration options
## 🌟 Why SecretFetch?

## Installation
Managing secrets in Go applications can be a pain. AWS Secrets Manager is powerful but complex. Environment variables are simple but limited. What if you could have the best of both worlds?

SecretFetch gives you:

- 🎯 **Dead Simple API** - Just add struct tags and go!
- 🔄 **Multi-Source Support** - AWS Secrets Manager, env vars, and fallbacks in one place
- 🚀 **Type Safety** - Automatic type conversion for strings, numbers, durations, and more
-**Performance** - Built-in caching to reduce AWS API calls
- 🛡️ **Validation** - Pattern matching and custom validators to catch issues early
- 🔧 **Flexibility** - Transform values, decode base64, parse JSON/YAML
- 🏃‍♂️ **Zero Config** - Works out of the box with sane defaults

## 🤔 The Problem

You're building a Go application and need to manage secrets. You have a few options:

1. **Hardcode them** (Please don't!)
2. **Use environment variables** (Manual management, no validation)
3. **Use AWS Secrets Manager directly** (Complex API, no caching, lots of boilerplate)
4. **Use SecretFetch** (Simple, flexible, and powerful!)

## 🚀 Quick Start

```bash
go get github.com/crazywolf132/SecretFetch
```

## Basic Usage

```go
type Config struct {
// Basic string value from AWS Secrets Manager or environment
APIKey string `secret:"aws=prod/api/key,env=API_KEY,required"`
// Get from AWS, fallback to env var
APIKey string `secret:"aws=prod/api/key,env=API_KEY"`

// Number with fallback value
MaxRetries int `secret:"env=MAX_RETRIES,fallback=3"`
// Validate email format
Email string `secret:"env=EMAIL,pattern=^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"`

// Duration with pattern validation
Timeout time.Duration `secret:"env=TIMEOUT,fallback=30s,pattern=^[0-9]+[smh]$"`
// Parse duration with fallback
Timeout time.Duration `secret:"env=TIMEOUT,fallback=30s"`
}

// Create and populate your config
cfg := &Config{}
if err := secretfetch.Fetch(context.Background(), cfg, nil); err != nil {
log.Fatal(err)
}
```

## Advanced Features
## 🎯 Features Deep Dive

### Pattern Validation
```go
type Config struct {
// Email validation
Email string `secret:"env=EMAIL,pattern=^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"`

// IP address validation
IPAddr string `secret:"env=IP_ADDR,pattern=((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"`

// Port number validation
Port string `secret:"env=PORT,pattern=^(6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3})$"`

// Version string validation
Version string `secret:"env=VERSION,pattern=v[0-9]+\\.[0-9]+\\.[0-9]+"`
}
```
### 🔐 AWS Secrets Manager Integration

### Custom Validation
```go
type Config struct {
Password string `secret:"env=PASSWORD"`
type DatabaseConfig struct {
Host string `json:"host"`
Username string `json:"username"`
Password string `json:"password"`
}

opts := &secretfetch.Options{
Validators: map[string]secretfetch.ValidatorFunc{
"PASSWORD": func(value string) error {
if len(value) < 8 {
return fmt.Errorf("password must be at least 8 characters")
}
return nil
},
},
type Config struct {
// Parse entire database config from AWS Secrets Manager
DB DatabaseConfig `secret:"aws=prod/db/config,json"`
}
```

### Value Transformation
### 🔍 Pattern Validation

```go
type Config struct {
// Transform value before use
APIKey string `secret:"env=API_KEY"`
// Validate IP address format
IPAddr string `secret:"env=IP,pattern=((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"`

// Validate semantic version
Version string `secret:"env=VERSION,pattern=v[0-9]+\\.[0-9]+\\.[0-9]+"`
}
```

### 🔄 Value Transformation

```go
opts := &secretfetch.Options{
Transformers: map[string]secretfetch.TransformerFunc{
"API_KEY": func(value string) (string, error) {
Expand All @@ -99,36 +95,8 @@ opts := &secretfetch.Options{
}
```

### Complex Configuration with JSON/YAML
```go
type DatabaseConfig struct {
Host string `json:"host"`
Port int `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
}

type Config struct {
// Parse entire database config from JSON
Database DatabaseConfig `secret:"aws=prod/db/config,json"`

// Parse array from JSON
AllowedIPs []string `secret:"env=ALLOWED_IPS,json"`
}
```

### Base64 Decoding and Binary Data
```go
type Config struct {
// Automatically decode base64-encoded certificate
Certificate string `secret:"env=TLS_CERT,base64"`

// Store as raw bytes
PrivateKey []byte `secret:"env=PRIVATE_KEY,base64"`
}
```
### ⚡ Smart Caching

### Caching with TTL
```go
type Config struct {
// Cache for 5 minutes
Expand All @@ -137,67 +105,45 @@ type Config struct {
// Cache indefinitely
StaticConfig string `secret:"aws=prod/static/config,ttl=-1"`
}

// Global caching options
opts := &secretfetch.Options{
DefaultTTL: 5 * time.Minute, // Default cache duration
Prefix: "MYAPP_", // Prefix for all env vars
}
```

### AWS Configuration
```go
opts := &secretfetch.Options{
AWSConfig: aws.Config{
Region: "us-west-2",
Credentials: credentials.NewStaticCredentialsProvider("ACCESS_KEY", "SECRET_KEY", ""),
},
}
```
## 🏆 Why Better Than Alternatives?

### Error Handling
```go
if err := secretfetch.Fetch(context.Background(), cfg, opts); err != nil {
switch e := err.(type) {
case *secretfetch.ValidationError:
log.Printf("Validation failed: %v", e)
case *secretfetch.PatternError:
log.Printf("Pattern match failed: %v", e)
case *secretfetch.RequiredError:
log.Printf("Required value missing: %v", e)
default:
log.Printf("Unknown error: %v", err)
}
}
```
### vs Direct AWS SDK
- 📉 **Less Code** - No more AWS boilerplate
- 🚀 **Built-in Caching** - Reduce API calls automatically
- 🎯 **Type Safety** - Automatic type conversion
-**Validation** - Catch issues before they hit production

### vs Environment Variables
- 🔐 **Multi-Source** - Use AWS for production, env vars for development
- 🛡️ **Validation** - Pattern matching and custom validators
- 🔄 **Transformation** - Process values before use
- 📦 **Structured Data** - Parse JSON/YAML automatically

## Best Practices
### vs Other Libraries
- 🎯 **Simple API** - Just use struct tags
- 🚀 **Performance** - Smart caching built-in
- 🔧 **Flexible** - Multiple sources, validation, transformation
- 📚 **Well Documented** - Comprehensive examples and guides

1. **Security**:
- Never log sensitive values
- Use environment variables for local development
- Rotate secrets regularly
- Use AWS IAM roles with minimal permissions
## 🛠️ Advanced Usage

2. **Performance**:
- Enable caching for frequently accessed values
- Use appropriate TTL values based on your needs
- Group related secrets in JSON objects to reduce AWS API calls
For a comprehensive technical deep-dive into all features and capabilities, check out our [Technical Documentation](TECHNICAL.md).

3. **Validation**:
- Always validate critical configuration values
- Use pattern matching for structured data
- Implement custom validators for complex rules
Additional resources in our [Wiki](https://github.com/crazywolf132/SecretFetch/wiki):

4. **Error Handling**:
- Check for specific error types
- Provide clear error messages
- Fail fast on missing required values
- Custom Validation Functions
- AWS Configuration Options
- Caching Strategies
- Error Handling
- Testing Strategies
- Best Practices

## Contributing
## 🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.
We love contributions! Check out our [Contributing Guide](CONTRIBUTING.md) to get started.

## License
## 📝 License

This project is licensed under the MIT License - see the LICENSE file for details.
MIT © [Brayden](LICENSE)
Loading

0 comments on commit 0883536

Please sign in to comment.