Skip to content

Commit

Permalink
mirror at 20220810193621
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Aug 10, 2022
1 parent 86ecdde commit 466802c
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 40 deletions.
1 change: 1 addition & 0 deletions api/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type DomainVerificationOwnership struct {
}

type DomainVerificationSSL struct {
Pending bool `json:"pending"`
Records []DomainVerificationSSLRecord `json:"records"`
Errors []string `json:"errors"`
}
Expand Down
79 changes: 71 additions & 8 deletions api/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package api

import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

Expand All @@ -15,11 +17,72 @@ type Email interface {
}

type EmailSend struct {
Project string `json:"project" yaml:"project"`
From string `json:"from" yaml:"from"`
To []string `json:"to" yaml:"to"`
Subject string `json:"subject" yaml:"subject"`
Body EmailBody `json:"body" yaml:"body"`
Project string `json:"project" yaml:"project"`
From EmailAddr `json:"from" yaml:"from"`
To []EmailAddr `json:"to" yaml:"to"`
Subject string `json:"subject" yaml:"subject"`
Body EmailBody `json:"body" yaml:"body"`
}

type EmailAddr struct {
Email string `json:"email" yaml:"email"`
Name string `json:"name" yaml:"name"`
}

func (a *EmailAddr) Valid() bool {
a.Email = strings.TrimSpace(a.Email)
a.Name = strings.TrimSpace(a.Name)

if a.Email == "" {
return false
}
if !govalidator.IsEmail(a.Email) {
return false
}
return true
}

func (a *EmailAddr) IsZero() bool {
return a.Email == ""
}

func (a EmailAddr) Address() string {
if a.Name == "" {
return a.Email
}
return fmt.Sprintf("%s <%s>", a.Name, a.Email)
}

func (a *EmailAddr) UnmarshalJSON(b []byte) error {
*a = EmailAddr{}

if len(b) == 0 {
return fmt.Errorf("empty address")
}

switch string(b[0]) {
case "{":
var t struct {
Email string `json:"email"`
Name string `json:"name"`
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
a.Email = t.Email
a.Name = t.Name
case "\"":
var t string
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
a.Email = t
default:
return fmt.Errorf("invalid address")
}
return nil
}

type EmailBody struct {
Expand All @@ -45,12 +108,12 @@ func (m *EmailSend) Valid() error {
m.Subject = strings.TrimSpace(m.Subject)

v.Must(m.Project != "", "project required")
if v.Must(m.From != "", "from required") {
v.Must(govalidator.IsEmail(m.From), "from invalid")
if v.Must(!m.From.IsZero(), "from required") {
v.Must(m.From.Valid(), "from invalid")
}
if v.Must(len(m.To) > 0, "to required") {
for _, to := range m.To {
v.Mustf(govalidator.IsEmail(to), "to '%s' invalid", to)
v.Mustf(to.Valid(), "to '%s' invalid", to.Address())
}
}
v.Must(m.Subject != "", "subject required")
Expand Down
1 change: 1 addition & 0 deletions api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
ErrProjectNotFound = newError("api: project not found")
ErrBillingAccountNotFound = newError("api: billing account not found")
ErrBillingAccountNotActive = newError("api: billing account not active, please contact us via email to activate billing account")
ErrBillingAccountInUsed = newError("api: billing account in used")
ErrDeploymentNotFound = newError("api: deployment not found")
ErrInvalidRouteTarget = newError("api: invalid route target")
ErrCanNotDelete = newError("api: can not delete")
Expand Down
17 changes: 11 additions & 6 deletions api/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ type ProjectGet struct {
}

type ProjectItem struct {
ID int64 `json:"id" yaml:"id"`
Project string `json:"project" yaml:"project"`
Name string `json:"name" yaml:"name"`
BillingAccount string `json:"billingAccount" yaml:"billingAccount"`
Quota ProjectQuota `json:"quota" yaml:"quota"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
ID int64 `json:"id" yaml:"id"`
Project string `json:"project" yaml:"project"`
Name string `json:"name" yaml:"name"`
BillingAccount string `json:"billingAccount" yaml:"billingAccount"`
Quota ProjectQuota `json:"quota" yaml:"quota"`
Config ProjectConfig `json:"config" yaml:"config"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
}

func (m *ProjectItem) Table() [][]string {
Expand All @@ -108,6 +109,10 @@ type ProjectQuota struct {
DeploymentMaxReplicas int `json:"deploymentMaxReplicas" yaml:"deploymentMaxReplicas"`
}

type ProjectConfig struct {
DomainCloudflare bool `json:"domainCloudflare" yaml:"domainCloudflare"`
}

type ProjectListResult struct {
Items []*ProjectItem `json:"items" yaml:"items"`
}
Expand Down
4 changes: 2 additions & 2 deletions api/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ func (m *RoleCreate) Valid() error {
v.Must(ReValidSID.MatchString(m.Role), "role invalid")
{
cnt := utf8.RuneCountInString(m.Role)
v.Must(cnt >= 6 && cnt <= 20, "role must have length between 6-20 characters")
v.Must(cnt >= 3 && cnt <= 20, "role must have length between 3-20 characters")
}
v.Must(utf8.ValidString(m.Name), "name invalid")
{
cnt := utf8.RuneCountInString(m.Name)
v.Must(cnt >= 4 && cnt <= 64, "name must have length between 4-64 characters")
v.Must(cnt >= 3 && cnt <= 64, "name must have length between 3-64 characters")
}

return WrapValidate(v)
Expand Down
42 changes: 30 additions & 12 deletions api/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,23 @@ func (m *RouteCreate) Valid() error {
return WrapValidate(v)
}

type RouteConfig struct {
ForwardAuth *RouteConfigForwardAuth `json:"forwardAuth" yaml:"forwardAuth"`
}

type RouteConfigForwardAuth struct {
Target string `json:"target" yaml:"target"`
AuthRequestHeaders []string `json:"authRequestHeaders" yaml:"authRequestHeaders"`
AuthResponseHeaders []string `json:"authResponseHeaders" yaml:"authResponseHeaders"`
}

type RouteCreateV2 struct {
Project string `json:"project" yaml:"project"`
Location string `json:"location" yaml:"location"`
Domain string `json:"domain" yaml:"domain"`
Path string `json:"path" yaml:"path"`
Target string `json:"target" yaml:"target"`
Project string `json:"project" yaml:"project"`
Location string `json:"location" yaml:"location"`
Domain string `json:"domain" yaml:"domain"`
Path string `json:"path" yaml:"path"`
Target string `json:"target" yaml:"target"`
Config RouteConfig `json:"config" yaml:"config"`
}

func (m *RouteCreateV2) Valid() error {
Expand All @@ -73,6 +84,12 @@ func (m *RouteCreateV2) Valid() error {
}
v.Must(validRouteTarget(m.Target), "target invalid")

if m.Config.ForwardAuth != nil {
if v.Must(m.Config.ForwardAuth.Target != "", "target required") {
v.Must(validURL(m.Config.ForwardAuth.Target), "target invalid")
}
}

return WrapValidate(v)
}

Expand Down Expand Up @@ -129,13 +146,14 @@ func (m *RouteListResult) Table() [][]string {
}

type RouteItem struct {
Location string `json:"location" yaml:"location"`
Domain string `json:"domain" yaml:"domain"`
Path string `json:"path" yaml:"path"`
Target string `json:"target" yaml:"target"`
Deployment string `json:"deployment" yaml:"deployment"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
CreatedBy string `json:"createdBy" yaml:"createdBy"`
Location string `json:"location" yaml:"location"`
Domain string `json:"domain" yaml:"domain"`
Path string `json:"path" yaml:"path"`
Target string `json:"target" yaml:"target"`
Deployment string `json:"deployment" yaml:"deployment"`
Config RouteConfig `json:"config" yaml:"config"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
CreatedBy string `json:"createdBy" yaml:"createdBy"`
}

func (m *RouteItem) Table() [][]string {
Expand Down
4 changes: 2 additions & 2 deletions api/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (m *ServiceAccountCreate) Valid() error {
if v.Must(m.SID != "", "sid required") {
v.Mustf(ReValidSID.MatchString(m.SID), "sid invalid %s", ReValidSIDStr)
cnt := utf8.RuneCountInString(m.SID)
v.Must(cnt >= 6 && cnt <= 20, "sid must have length between 6-20 characters")
v.Must(cnt >= 3 && cnt <= 20, "sid must have length between 3-20 characters")
}
{
cnt := utf8.RuneCountInString(m.Name)
Expand Down Expand Up @@ -65,7 +65,7 @@ func (m *ServiceAccountUpdate) Valid() error {
if v.Must(m.SID != "", "sid required") {
v.Mustf(ReValidSID.MatchString(m.SID), "sid invalid %s", ReValidSIDStr)
cnt := utf8.RuneCountInString(m.SID)
v.Must(cnt >= 6 && cnt <= 20, "sid must have length between 6-20 characters")
v.Must(cnt >= 3 && cnt <= 20, "sid must have length between 3-20 characters")
}
{
cnt := utf8.RuneCountInString(m.Name)
Expand Down
10 changes: 9 additions & 1 deletion api/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"strings"

"github.com/asaskevich/govalidator"
"github.com/moonrhythm/validator"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ func IsValidateError(err error) bool {

// helper

var reEnvName = regexp.MustCompile(`[-._a-zA-Z][-._a-zA-Z0-9]*`)
var reEnvName = regexp.MustCompile(`^[-._a-zA-Z][-._a-zA-Z0-9]*$`)

func validEnvName(env map[string]string) bool {
for k := range env {
Expand All @@ -71,3 +72,10 @@ func validRouteTarget(target string) bool {
}
return false
}

func validURL(url string) bool {
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
return govalidator.IsURL(url)
}
return false
}
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ require (
require (
cloud.google.com/go/compute v1.6.1 // indirect
github.com/acoshift/hrpc/v3 v3.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/stretchr/testify v1.7.1 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
github.com/stretchr/testify v1.8.0 // indirect
golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
12 changes: 7 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBO
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -295,8 +297,8 @@ golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220325170049-de3da57026de/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220412020605-290c469a71a5/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 h1:NWy5+hlRbC7HK+PmcXVUmW1IMyFce7to56IUvhUFm7Y=
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220708220712-1185a9018129 h1:vucSRfWwTsoXro7P+3Cjlr6flUMtzCwzlvkxEQtHHB0=
golang.org/x/net v0.0.0-20220708220712-1185a9018129/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -621,8 +623,8 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down

0 comments on commit 466802c

Please sign in to comment.