Skip to content

Commit 5d3bc0f

Browse files
committed
Refactor acme.sh dns providers
- updated chakra and typescript - added locales for dns provider configs
1 parent 1d5d3ec commit 5d3bc0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2207
-2164
lines changed

backend/internal/api/handler/dns_providers.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ func DeleteDNSProvider() func(http.ResponseWriter, *http.Request) {
133133
// Route: GET /dns-providers/acmesh
134134
func GetAcmeshProviders() func(http.ResponseWriter, *http.Request) {
135135
return func(w http.ResponseWriter, r *http.Request) {
136-
items := dnsproviders.List()
137-
h.ResultResponseJSON(w, r, http.StatusOK, items)
136+
h.ResultResponseJSON(w, r, http.StatusOK, dnsproviders.List())
138137
}
139138
}
140139

backend/internal/api/schema/create_dns_provider.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"npm/internal/dnsproviders"
8+
"npm/internal/logger"
89
"npm/internal/util"
910
)
1011

@@ -15,7 +16,12 @@ func CreateDNSProvider() string {
1516

1617
allSchemasWrapped := make([]string, 0)
1718
for providerName, provider := range allProviders {
18-
allSchemasWrapped = append(allSchemasWrapped, createDNSProviderType(providerName, provider.Schema))
19+
schema, err := provider.GetJsonSchema()
20+
if err != nil {
21+
logger.Error("ProviderSchemaError", fmt.Errorf("Invalid Provider Schema for %s: %v", provider.Title, err))
22+
} else {
23+
allSchemasWrapped = append(allSchemasWrapped, createDNSProviderType(providerName, schema))
24+
}
1925
}
2026

2127
return fmt.Sprintf(fmtStr, util.ConvertStringSliceToInterface(allSchemasWrapped)...)
Lines changed: 32 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
package dnsproviders
22

33
import (
4+
"encoding/json"
45
"errors"
5-
"npm/internal/util"
66
)
77

8+
// providerField should mimick jsonschema, so that
9+
// the ui can render a field and validate it
10+
// before we do.
11+
// See: https://json-schema.org/draft/2020-12/json-schema-validation.html
812
type providerField struct {
9-
Name string `json:"name"`
10-
Type string `json:"type"`
11-
IsRequired bool `json:"is_required"`
12-
IsSecret bool `json:"is_secret"`
13-
MetaKey string `json:"meta_key"`
14-
EnvKey string `json:"-"` // not exposed in api
13+
Title string `json:"title"`
14+
Type string `json:"type"`
15+
AdditionalProperties bool `json:"additionalProperties"`
16+
Minimum int `json:"minimum,omitempty"`
17+
Maximum int `json:"maximum,omitempty"`
18+
MinLength int `json:"minLength,omitempty"`
19+
MaxLength int `json:"maxLength,omitempty"`
20+
Pattern string `json:"pattern,omitempty"`
21+
IsSecret bool `json:"isSecret"` // Not valid jsonschema
1522
}
1623

1724
// Provider is a simple struct
1825
type Provider struct {
19-
AcmeshName string `json:"acmesh_name"`
20-
Schema string `json:"-"`
21-
Fields []providerField `json:"fields"`
26+
Title string `json:"title"`
27+
Type string `json:"type"` // Should always be "object"
28+
AdditionalProperties bool `json:"additionalProperties"`
29+
MinProperties int `json:"minProperties,omitempty"`
30+
Required []string `json:"required,omitempty"`
31+
Properties map[string]providerField `json:"properties"`
2232
}
2333

24-
// GetAcmeEnvVars will map the meta given to the env var required for
25-
// acme.sh to use this dns provider
26-
func (p *Provider) GetAcmeEnvVars(meta interface{}) map[string]string {
27-
res := make(map[string]string)
28-
for _, field := range p.Fields {
29-
if acmeShEnvValue, found := util.FindItemInInterface(field.MetaKey, meta); found {
30-
res[field.EnvKey] = acmeShEnvValue.(string)
31-
}
32-
}
33-
return res
34+
// GetJsonSchema encodes this object as JSON string
35+
func (p *Provider) GetJsonSchema() (string, error) {
36+
b, err := json.Marshal(p)
37+
return string(b), err
38+
}
39+
40+
// ConvertToUpdatable will manipulate this object so that it returns
41+
// an updatable json schema
42+
func (p *Provider) ConvertToUpdatable() {
43+
p.MinProperties = 1
44+
p.Required = nil
3445
}
3546

3647
// List returns an array of providers
@@ -89,7 +100,7 @@ func GetAll() map[string]Provider {
89100
mp := make(map[string]Provider)
90101
items := List()
91102
for _, item := range items {
92-
mp[item.AcmeshName] = item
103+
mp[item.Title] = item
93104
}
94105
return mp
95106
}
@@ -102,51 +113,3 @@ func Get(provider string) (Provider, error) {
102113
}
103114
return Provider{}, errors.New("provider_not_found")
104115
}
105-
106-
// GetAllSchemas returns a flat array with just the schemas
107-
func GetAllSchemas() []string {
108-
items := List()
109-
mp := make([]string, 0)
110-
for _, item := range items {
111-
mp = append(mp, item.Schema)
112-
}
113-
return mp
114-
}
115-
116-
const commonKeySchema = `
117-
{
118-
"type": "object",
119-
"required": [
120-
"api_key"
121-
],
122-
"additionalProperties": false,
123-
"properties": {
124-
"api_key": {
125-
"type": "string",
126-
"minLength": 1
127-
}
128-
}
129-
}
130-
`
131-
132-
// nolint: gosec
133-
const commonKeySecretSchema = `
134-
{
135-
"type": "object",
136-
"required": [
137-
"api_key",
138-
"secret"
139-
],
140-
"additionalProperties": false,
141-
"properties": {
142-
"api_key": {
143-
"type": "string",
144-
"minLength": 1
145-
},
146-
"secret": {
147-
"type": "string",
148-
"minLength": 1
149-
}
150-
}
151-
}
152-
`

backend/internal/dnsproviders/dns_acmedns.go

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,33 @@
11
package dnsproviders
22

3-
const acmeDNSchema = `
4-
{
5-
"type": "object",
6-
"required": [
7-
"api_url",
8-
"user",
9-
"password"
10-
],
11-
"additionalProperties": false,
12-
"properties": {
13-
"api_url": {
14-
"type": "string",
15-
"minLength": 4
16-
},
17-
"subdomain": {
18-
"type": "string",
19-
"minLength": 1
20-
},
21-
"user": {
22-
"type": "string",
23-
"minLength": 1
24-
},
25-
"password": {
26-
"type": "string",
27-
"minLength": 1
28-
}
29-
}
30-
}
31-
`
32-
333
func getDNSAcmeDNS() Provider {
344
return Provider{
35-
AcmeshName: "dns_acmedns",
36-
Schema: acmeDNSchema,
37-
Fields: []providerField{
38-
{
39-
Name: "Base URL",
40-
Type: "text",
41-
MetaKey: "api_url",
42-
EnvKey: "ACMEDNS_BASE_URL",
43-
IsRequired: true,
5+
Title: "dns_acmedns",
6+
Type: "object",
7+
AdditionalProperties: false,
8+
Required: []string{
9+
"ACMEDNS_BASE_URL",
10+
"ACMEDNS_SUBDOMAIN",
11+
"ACMEDNS_USERNAME",
12+
"ACMEDNS_PASSWORD",
13+
},
14+
Properties: map[string]providerField{
15+
"ACMEDNS_BASE_URL": {
16+
Title: "base-url",
17+
Type: "string",
4418
},
45-
{
46-
Name: "Subdomain",
47-
Type: "text",
48-
MetaKey: "subdomain",
49-
EnvKey: "ACMEDNS_SUBDOMAIN",
50-
IsRequired: true,
19+
"ACMEDNS_SUBDOMAIN": {
20+
Title: "subdomain",
21+
Type: "string",
5122
},
52-
{
53-
Name: "User",
54-
Type: "text",
55-
MetaKey: "user",
56-
EnvKey: "ACMEDNS_USERNAME",
57-
IsRequired: true,
23+
"ACMEDNS_USERNAME": {
24+
Title: "username",
25+
Type: "string",
5826
},
59-
{
60-
Name: "Password",
61-
Type: "password",
62-
MetaKey: "password",
63-
EnvKey: "ACMEDNS_PASSWORD",
64-
IsRequired: true,
65-
IsSecret: true,
27+
"ACMEDNS_PASSWORD": {
28+
Title: "password",
29+
Type: "string",
30+
IsSecret: true,
6631
},
6732
},
6833
}

backend/internal/dnsproviders/dns_ad.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package dnsproviders
22

33
func getDNSAd() Provider {
44
return Provider{
5-
AcmeshName: "dns_ad",
6-
Schema: commonKeySchema,
7-
Fields: []providerField{
8-
{
9-
Name: "API Key",
10-
Type: "password",
11-
MetaKey: "api_key",
12-
EnvKey: "AD_API_KEY",
13-
IsRequired: true,
5+
Title: "dns_ad",
6+
Type: "object",
7+
AdditionalProperties: false,
8+
Required: []string{
9+
"AD_API_KEY",
10+
},
11+
Properties: map[string]providerField{
12+
"AD_API_KEY": {
13+
Title: "api-key",
14+
Type: "string",
15+
MinLength: 1,
1416
},
1517
},
1618
}

backend/internal/dnsproviders/dns_ali.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@ package dnsproviders
22

33
func getDNSAli() Provider {
44
return Provider{
5-
AcmeshName: "dns_ali",
6-
Schema: commonKeySecretSchema,
7-
Fields: []providerField{
8-
{
9-
Name: "Key",
10-
Type: "text",
11-
MetaKey: "api_key",
12-
EnvKey: "Ali_Key",
13-
IsRequired: true,
5+
Title: "dns_ali",
6+
Type: "object",
7+
AdditionalProperties: false,
8+
Required: []string{
9+
"Ali_Key",
10+
"Ali_Secret",
11+
},
12+
Properties: map[string]providerField{
13+
"Ali_Key": {
14+
Title: "api-key",
15+
Type: "string",
16+
MinLength: 1,
1417
},
15-
{
16-
Name: "Secret",
17-
Type: "password",
18-
MetaKey: "secret",
19-
EnvKey: "Ali_Secret",
20-
IsRequired: true,
21-
IsSecret: true,
18+
"Ali_Secret": {
19+
Title: "secret",
20+
Type: "string",
21+
MinLength: 1,
22+
IsSecret: true,
2223
},
2324
},
2425
}

0 commit comments

Comments
 (0)