Documentation
¶
Overview ¶
Package validation provides the input checks that CLI commands share:
- Email address format
- UUID IDs and path arguments
- Domain name format
- Page limits
- API key scopes and IP allow lists
- API URLs and loopback hosts
A check of a flag or an argument returns a validation error from the errors package, which exits with 4; CheckAPIURL returns only the reason, which its callers put in their own error.
Index ¶
- Constants
- Variables
- func ASCIIDomain(domain string) string
- func CanonicalScope(scope string) string
- func CheckAPIURL(rawURL string) error
- func CheckHTTPURL(rawURL, example string) error
- func CheckIPAllowList(entries []string) error
- func InvalidValueError(name, value, reason string) error
- func IsDomainName(domain string) bool
- func IsEmailAddress(email string) bool
- func IsLoopbackHost(host string) bool
- func IsParentOnlyScope(scope string) bool
- func IsPrintableASCII(s string) bool
- func ParseMessageID(fieldName, value string) (string, error)
- func ParseUUIDField(fieldName, value string) (uuid.UUID, error)
- func ParseUUIDs(fieldName string, values []string) ([]uuid.UUID, error)
- func PlainHTTPWarning(subject, rawURL, data string) string
- func SendsInClear(rawURL string) bool
- func ValidateDomainName(name, domain string) error
- func ValidateDomainNames(name string, domains []string) error
- func ValidateEmail(name, email string) error
- func ValidateOptionalDomainName(name string, domain *string) error
- func ValidateOptionalEmail(name string, email *string) error
- func ValidatePageLimit(limit int32) error
- func ValidatePathSegment(fieldName, value string) error
- func ValidateScope(scope, helpCommand string) error
- func ValidateUUIDField(fieldName, value string) error
- type ScopeGroup
Constants ¶
const APIKeyNotPrintable = "must be printable ASCII, without control or non-ASCII characters"
APIKeyNotPrintable is the reason an API key with a character that is not printable ASCII is refused: no AhaSend API key holds one, and an HTTP header cannot carry it, so a request with it could only fail.
Variables ¶
var ScopeGroups = []ScopeGroup{ {"Messages", []string{ "messages:send:all", "messages:send:{domain}", "messages:cancel:all", "messages:cancel:{domain}", "messages:read:all", "messages:read:{domain}", }}, {"Domains", []string{"domains:read", "domains:write", "domains:delete:all", "domains:delete:{domain}"}}, {"Accounts", []string{ "accounts:read", "accounts:write", "accounts:billing", "accounts:members:read", "accounts:members:add", "accounts:members:update", "accounts:members:remove", }}, {"Webhooks", []string{ "webhooks:read:all", "webhooks:read:{domain}", "webhooks:write:all", "webhooks:write:{domain}", "webhooks:delete:all", "webhooks:delete:{domain}", }}, {"Routes", []string{ "routes:read:all", "routes:read:{domain}", "routes:write:all", "routes:write:{domain}", "routes:delete:all", "routes:delete:{domain}", }}, {"Suppressions", []string{"suppressions:read", "suppressions:write", "suppressions:delete", "suppressions:wipe"}}, {"Templates", []string{"templates:read", "templates:write", "templates:delete"}}, {"Campaigns", []string{"campaigns:read", "campaigns:write", "campaigns:delete", "campaigns:send"}}, {"Contacts", []string{"contacts:read", "contacts:write", "contacts:delete"}}, {"Lists", []string{"lists:read", "lists:write", "lists:delete"}}, {"Segments", []string{"segments:read", "segments:write", "segments:delete"}}, {"SMTP Credentials", []string{ "smtp-credentials:read:all", "smtp-credentials:read:{domain}", "smtp-credentials:write:all", "smtp-credentials:write:{domain}", "smtp-credentials:delete:all", "smtp-credentials:delete:{domain}", }}, {"Statistics", []string{"statistics-transactional:read:all", "statistics-transactional:read:{domain}"}}, {"API Keys", []string{"api-keys:read", "api-keys:write", "api-keys:delete"}}, {"Sub-Accounts", []string{ "sub-accounts:read", "sub-accounts:write", "sub-accounts:delete", "sub-accounts:suspend", "sub-accounts:usage", }}, {"Sub-Account API Keys", []string{ "sub-account-api-keys:read", "sub-account-api-keys:write", "sub-account-api-keys:delete", }}, }
ScopeGroups are the API key scopes the API accepts, grouped and ordered as the API reference lists them. The API key commands and the sub-account API key commands check and list scopes from this one list; a sub-account's key cannot have the parent-only scopes (see IsParentOnlyScope). A scope ending in {domain} takes one of the key's account's domains in its place, such as messages:send:{example.com}.
Functions ¶
func ASCIIDomain ¶ added in v0.3.0
ASCIIDomain returns domain with each label that is not ASCII written as "xn--" and its Punycode, as DNS resolves an international domain name, such as "xn--bcher-kva.de" for "bücher.de". ASCII labels are kept as they are. It does no other mapping or checking: a caller lowercases the domain first.
func CanonicalScope ¶ added in v0.3.0
CanonicalScope returns scope as the API compares scopes: the domain of a scope that takes one in lower case and as scopeDomain reads it.
func CheckAPIURL ¶ added in v0.3.0
CheckAPIURL returns why rawURL cannot be the AhaSend API URL, or nil. The API URL passes CheckHTTPURL and has nothing after its host: the API's paths start at the root of the host, so a path would be lost, and the API key is the only credential the CLI sends.
func CheckHTTPURL ¶ added in v0.3.0
CheckHTTPURL returns why rawURL is not an HTTP or HTTPS URL with a host and no user name or password, or nil; the reason ends with example, such as https://example.com/webhook. The CLI shows the URLs it takes, such as in the output of webhooks get, so a password in one would show too.
func CheckIPAllowList ¶ added in v0.3.0
CheckIPAllowList checks the --ip-allow-list values of an API key: each is a CIDR block or an IP address other than an allow-all prefix, and they make at most 100 distinct blocks.
func InvalidValueError ¶ added in v0.3.0
InvalidValueError is the validation error of value, the value of the input called name, a flag such as "--domain" or an argument such as "domain", with the reason it is invalid: `invalid --domain value "x": reason`.
func IsDomainName ¶ added in v0.3.0
IsDomainName reports whether domain is a domain name: dot-separated labels of letters, digits and inner hyphens, 253 characters at most.
func IsEmailAddress ¶ added in v0.3.0
IsEmailAddress reports whether email is a bare email address: one that net/mail parses as itself, with no display name, angle brackets, quotes or surrounding spaces. It accepts addresses such as o'brien@example.com and non-ASCII ones, which the mail format allows, and leaves deliverability to the API.
func IsLoopbackHost ¶ added in v0.3.0
IsLoopbackHost reports whether host, a host name or IP address without a port, is this machine: localhost or a loopback address such as 127.0.0.1 or ::1. Data sent to it never leaves the machine.
func IsParentOnlyScope ¶ added in v0.3.0
IsParentOnlyScope reports whether scope manages sub-accounts or their keys, which the API refuses to a key that a sub-account owns.
func IsPrintableASCII ¶ added in v0.3.0
IsPrintableASCII reports whether s holds only printable ASCII: letters, digits, punctuation and spaces, without control or non-ASCII characters.
func ParseMessageID ¶ added in v0.3.0
ParseMessageID checks a message ID argument, which is a UUID or a UUID and a domain in angle brackets, such as <uuid@example.com>. It returns the ID to send to the API.
func ParseUUIDField ¶ added in v0.3.0
ParseUUIDField parses a named field that must contain a UUID, returning the same errors as ValidateUUIDField.
func ParseUUIDs ¶ added in v0.3.0
ParseUUIDs parses each of values, IDs of the field fieldName such as "list ID", like ParseUUIDField. It returns nil for no values.
func PlainHTTPWarning ¶ added in v0.3.0
PlainHTTPWarning is the warning about rawURL, a URL that SendsInClear: subject names it, such as "the API URL" or "--url", and data is what it sends unencrypted, such as "the API key".
func SendsInClear ¶ added in v0.3.0
SendsInClear reports whether rawURL sends data over plain HTTP to a host that is not a loopback host.
func ValidateDomainName ¶
ValidateDomainName validates domain, the value of the input called name: a flag such as "--domain" or an argument such as "domain".
func ValidateDomainNames ¶ added in v0.3.0
ValidateDomainNames validates each of domains like ValidateDomainName.
func ValidateEmail ¶
ValidateEmail checks that email, the value of the input name, a flag such as "--from" or an argument such as "email", is an email address (see IsEmailAddress): `invalid --from value "x": not an email address`. An empty value, or one of only white space, fails with `--from cannot be empty`.
func ValidateOptionalDomainName ¶ added in v0.3.0
ValidateOptionalDomainName validates domain like ValidateDomainName, when there is one.
func ValidateOptionalEmail ¶ added in v0.3.0
ValidateOptionalEmail validates email like ValidateEmail, when there is one.
func ValidatePageLimit ¶
ValidatePageLimit validates an optional page limit
func ValidatePathSegment ¶ added in v0.3.0
ValidatePathSegment validates that the argument fieldName, such as "domain" or "contact ID or email", can address one path segment: not empty or blank, not a dot segment and free of control characters, the values the SDK refuses to put in a path or that address nothing. Its format is left to the API.
func ValidateScope ¶
ValidateScope checks that scope, a value of --scope, is one of ScopeGroups, with a domain name in place of {domain}. An unknown scope's error points to the help of helpCommand, which lists the scopes.
func ValidateUUIDField ¶
ValidateUUIDField validates that a named field contains a valid UUID format
Types ¶
type ScopeGroup ¶ added in v0.3.0
ScopeGroup is a group of API key scopes, such as the scopes of messages.