Skip to content

Commit

Permalink
add configuration validation for webhook (#15)
Browse files Browse the repository at this point in the history
meanwhile, the webhook's error message is now clearer and more precise.
  • Loading branch information
wjiec authored Mar 21, 2024
1 parent f847011 commit 345d1cc
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ indent_style = tab

[{*.yaml,*.yml,*.json}]
indent_size = 2

[VERSION]
insert_final_newline = false
4 changes: 1 addition & 3 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ on:
push:
branches:
- main
tags:
- 'v*'

jobs:
test:
Expand All @@ -19,7 +17,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Go ${{ matrix.go-version }}
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ The following table lists the correspondences between alidns-webhook and k8s ver

| Alidns-Webhook version | k8s supported version | Helm Chart Version |
|------------------------|------------------------|--------------------|
| **v1.0.0** | 1.29, 1.28, 1.27, 1.26 | 1.0.* |
| **v0.1.0** | 1.29, 1.28, 1.27, 1.26 | 0.1.* |


Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
1.0.0
2 changes: 1 addition & 1 deletion alidns.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (s *AliSolver) loadAliDNS(challenge *acme.ChallengeRequest) (*AliDNS, error
return nil, err
}

accessKeySecret, err := s.loadSecretData(cfg.SecretAccessKeyRef, challenge.ResourceNamespace)
accessKeySecret, err := s.loadSecretData(cfg.AccessKeySecretRef, challenge.ResourceNamespace)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion charts/alidns-webhook/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0
version: 1.0.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
16 changes: 8 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ import (
// be used by your provider here, you should reference a Kubernetes Secret
// resource and fetch these credentials using a Kubernetes clientset.
type Config struct {
Region string `json:"region"` // optional
AccessKeyIdRef cmmeta.SecretKeySelector `json:"accessKeyIdRef"`
// AccessKeySecretRef will serve as the alias name for SecretAccessKeyRef
Region string `json:"region"` // optional
AccessKeyIdRef cmmeta.SecretKeySelector `json:"accessKeyIdRef"`
AccessKeySecretRef cmmeta.SecretKeySelector `json:"accessKeySecretRef"`
// SecretAccessKeyRef will serve as the alias name for AccessKeySecretRef
SecretAccessKeyRef cmmeta.SecretKeySelector `json:"secretAccessKeyRef"`
}

// Validate checks if the config of the webhook is valid.
func (cfg *Config) Validate() error {
if len(cfg.AccessKeyIdRef.Name) == 0 {
return errors.New("testAccessKeyIdRef may not be empty")
return errors.New("accessKeyIdRef may not be empty")
}

if len(cfg.SecretAccessKeyRef.Name) == 0 {
cfg.AccessKeySecretRef.DeepCopyInto(&cfg.SecretAccessKeyRef)
if len(cfg.AccessKeySecretRef.Name) == 0 {
cfg.SecretAccessKeyRef.DeepCopyInto(&cfg.AccessKeySecretRef)
}
if len(cfg.SecretAccessKeyRef.Name) == 0 {
return errors.New("AccessKeySecretRef may not be empty")
if len(cfg.AccessKeySecretRef.Name) == 0 {
return errors.New("accessKeySecretRef may not be empty")
}

return nil
Expand Down
12 changes: 6 additions & 6 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ var (
},
Key: "access-key-id",
}
testSecretAccessKeyRef = cmmeta.SecretKeySelector{
testAccessKeySecretRef = cmmeta.SecretKeySelector{
LocalObjectReference: cmmeta.LocalObjectReference{
Name: "alidns-secret",
},
Key: "secret-access-key",
Key: "access-key-secret",
}
)

func TestConfig_Validate(t *testing.T) {
t.Run("happy", func(t *testing.T) {
correct := &Config{
AccessKeyIdRef: testAccessKeyIdRef,
SecretAccessKeyRef: testSecretAccessKeyRef,
AccessKeySecretRef: testAccessKeySecretRef,
}

loaded, err := loadConfig(&extapi.JSON{Raw: mustMarshal(correct)})
Expand All @@ -43,7 +43,7 @@ func TestConfig_Validate(t *testing.T) {
t.Run("compatible", func(t *testing.T) {
correct := &Config{
AccessKeyIdRef: testAccessKeyIdRef,
AccessKeySecretRef: testSecretAccessKeyRef,
SecretAccessKeyRef: testAccessKeySecretRef,
}

loaded, err := loadConfig(&extapi.JSON{Raw: mustMarshal(correct)})
Expand All @@ -59,14 +59,14 @@ func TestConfig_Validate(t *testing.T) {

t.Run("no accessKeyId", func(t *testing.T) {
bad := &Config{
SecretAccessKeyRef: testSecretAccessKeyRef,
SecretAccessKeyRef: testAccessKeySecretRef,
}

_, err := loadConfig(&extapi.JSON{Raw: mustMarshal(bad)})
assert.Error(t, err)
})

t.Run("no secretAccessKey", func(t *testing.T) {
t.Run("no accessKeySecret", func(t *testing.T) {
bad := &Config{
AccessKeyIdRef: testAccessKeyIdRef,
}
Expand Down

0 comments on commit 345d1cc

Please sign in to comment.