By defintion, the role of an adapter is to fit or convert one form onto another. Pertaining to Caddy, the adapter ingests a custom configuration format and should output the JSON equivalent of Caddy configuration. As with other extensions of Caddy, building an adapter involves implementing an interface. In this case, the interface consists of a single method and resides in the github.com/caddyserver/caddy/v2/caddyconfig1 package:
type Adapter interface {
Adapt(body []byte, options map[string]interface{}) ([]byte, []Warning, error)
}
Note how the signature of the method illustrates its intended behavior: it receives a blob, and returns a blob. Adapter modules must also be registered in an init() function as with other modules, except this registration function resides in the caddyconfig2 package rather than at the root of the Caddy Go module:
RegisterAdapter(name string, adapter Adapter)
Caddy comes with the caddyfile adapter as part of its standard offering.
Wrie a Caddy configuration adapter that takes a file containing base64 string of the JSON configuratin, decode the data, and return the result.
Here is test data:
ewoJImxvZ2dpbmciOiB7CgkJImxvZ3MiOiB7CgkJCSJkZWZhdWx0IjogewoJCQkJImxldmVsIjogIkRFQlVHIgoJCQl9CgkJfQoJfSwKCSJhcHBzIjogewoJCSJodHRwIjogewoJCQkic2VydmVycyI6IHsKCQkJCSJzcnYwIjogewoJCQkJCSJsaXN0ZW4iOiBbCgkJCQkJCSI6NDQzIgoJCQkJCV0sCgkJCQkJInJvdXRlcyI6IFsKCQkJCQkJewoJCQkJCQkJIm1hdGNoIjogWwoJCQkJCQkJCXsKCQkJCQkJCQkJImhvc3QiOiBbCgkJCQkJCQkJCQkibG9jYWxob3N0IgoJCQkJCQkJCQldCgkJCQkJCQkJfQoJCQkJCQkJXSwKCQkJCQkJCSJoYW5kbGUiOiBbCgkJCQkJCQkJewoJCQkJCQkJCQkiaGFuZGxlciI6ICJmaWxlX3NlcnZlciIKCQkJCQkJCQl9CgkJCQkJCQldLAoJCQkJCQkJInRlcm1pbmFsIjogdHJ1ZQoJCQkJCQl9CgkJCQkJXQoJCQkJfQoJCQl9CgkJfQoJfQp9
Write a Caddy configuration adapter that takes a file containing configuration that is encrypted using age3, decrypts it into its original JSON form, and returns the result.
To test, you may use this age key:
# created: 2022-07-28T03:50:31+03:00
# public key: age1vtmvdhu3dxfrmwlrl2dcucyt23dypnvt0msemzsefwt65m92rvtsjnmvxu
AGE-SECRET-KEY-1SK0KZTFN2WR9NTNXC7DGGFTNY0KL49WPV2QEM3M8F9SSTK9X32SSSZA7KC
Here’s a sample input encrypted using the above public key: link.