configuration.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. package configuration
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "net/http"
  7. "reflect"
  8. "strings"
  9. "time"
  10. )
  11. // Configuration is a versioned registry configuration, intended to be provided by a yaml file, and
  12. // optionally modified by environment variables.
  13. //
  14. // Note that yaml field names should never include _ characters, since this is the separator used
  15. // in environment variable names.
  16. type Configuration struct {
  17. // Version is the version which defines the format of the rest of the configuration
  18. Version Version `yaml:"version"`
  19. // Log supports setting various parameters related to the logging
  20. // subsystem.
  21. Log struct {
  22. // Level is the granularity at which registry operations are logged.
  23. Level Loglevel `yaml:"level"`
  24. // Formatter overrides the default formatter with another. Options
  25. // include "text", "json" and "logstash".
  26. Formatter string `yaml:"formatter,omitempty"`
  27. // Fields allows users to specify static string fields to include in
  28. // the logger context.
  29. Fields map[string]interface{} `yaml:"fields,omitempty"`
  30. // Hooks allows users to configurate the log hooks, to enabling the
  31. // sequent handling behavior, when defined levels of log message emit.
  32. Hooks []LogHook `yaml:"hooks,omitempty"`
  33. }
  34. // Loglevel is the level at which registry operations are logged. This is
  35. // deprecated. Please use Log.Level in the future.
  36. Loglevel Loglevel `yaml:"loglevel,omitempty"`
  37. // Storage is the configuration for the registry's storage driver
  38. Storage Storage `yaml:"storage"`
  39. // Auth allows configuration of various authorization methods that may be
  40. // used to gate requests.
  41. Auth Auth `yaml:"auth,omitempty"`
  42. // Middleware lists all middlewares to be used by the registry.
  43. Middleware map[string][]Middleware `yaml:"middleware,omitempty"`
  44. // Reporting is the configuration for error reporting
  45. Reporting Reporting `yaml:"reporting,omitempty"`
  46. // HTTP contains configuration parameters for the registry's http
  47. // interface.
  48. HTTP struct {
  49. // Addr specifies the bind address for the registry instance.
  50. Addr string `yaml:"addr,omitempty"`
  51. // Net specifies the net portion of the bind address. A default empty value means tcp.
  52. Net string `yaml:"net,omitempty"`
  53. // Host specifies an externally-reachable address for the registry, as a fully
  54. // qualified URL.
  55. Host string `yaml:"host,omitempty"`
  56. Prefix string `yaml:"prefix,omitempty"`
  57. // Secret specifies the secret key which HMAC tokens are created with.
  58. Secret string `yaml:"secret,omitempty"`
  59. // RelativeURLs specifies that relative URLs should be returned in
  60. // Location headers
  61. RelativeURLs bool `yaml:"relativeurls,omitempty"`
  62. // TLS instructs the http server to listen with a TLS configuration.
  63. // This only support simple tls configuration with a cert and key.
  64. // Mostly, this is useful for testing situations or simple deployments
  65. // that require tls. If more complex configurations are required, use
  66. // a proxy or make a proposal to add support here.
  67. TLS struct {
  68. // Certificate specifies the path to an x509 certificate file to
  69. // be used for TLS.
  70. Certificate string `yaml:"certificate,omitempty"`
  71. // Key specifies the path to the x509 key file, which should
  72. // contain the private portion for the file specified in
  73. // Certificate.
  74. Key string `yaml:"key,omitempty"`
  75. // Specifies the CA certs for client authentication
  76. // A file may contain multiple CA certificates encoded as PEM
  77. ClientCAs []string `yaml:"clientcas,omitempty"`
  78. } `yaml:"tls,omitempty"`
  79. // Headers is a set of headers to include in HTTP responses. A common
  80. // use case for this would be security headers such as
  81. // Strict-Transport-Security. The map keys are the header names, and
  82. // the values are the associated header payloads.
  83. Headers http.Header `yaml:"headers,omitempty"`
  84. // Debug configures the http debug interface, if specified. This can
  85. // include services such as pprof, expvar and other data that should
  86. // not be exposed externally. Left disabled by default.
  87. Debug struct {
  88. // Addr specifies the bind address for the debug server.
  89. Addr string `yaml:"addr,omitempty"`
  90. } `yaml:"debug,omitempty"`
  91. } `yaml:"http,omitempty"`
  92. // Notifications specifies configuration about various endpoint to which
  93. // registry events are dispatched.
  94. Notifications Notifications `yaml:"notifications,omitempty"`
  95. // Redis configures the redis pool available to the registry webapp.
  96. Redis struct {
  97. // Addr specifies the the redis instance available to the application.
  98. Addr string `yaml:"addr,omitempty"`
  99. // Password string to use when making a connection.
  100. Password string `yaml:"password,omitempty"`
  101. // DB specifies the database to connect to on the redis instance.
  102. DB int `yaml:"db,omitempty"`
  103. DialTimeout time.Duration `yaml:"dialtimeout,omitempty"` // timeout for connect
  104. ReadTimeout time.Duration `yaml:"readtimeout,omitempty"` // timeout for reads of data
  105. WriteTimeout time.Duration `yaml:"writetimeout,omitempty"` // timeout for writes of data
  106. // Pool configures the behavior of the redis connection pool.
  107. Pool struct {
  108. // MaxIdle sets the maximum number of idle connections.
  109. MaxIdle int `yaml:"maxidle,omitempty"`
  110. // MaxActive sets the maximum number of connections that should be
  111. // opened before blocking a connection request.
  112. MaxActive int `yaml:"maxactive,omitempty"`
  113. // IdleTimeout sets the amount time to wait before closing
  114. // inactive connections.
  115. IdleTimeout time.Duration `yaml:"idletimeout,omitempty"`
  116. } `yaml:"pool,omitempty"`
  117. } `yaml:"redis,omitempty"`
  118. Health Health `yaml:"health,omitempty"`
  119. Proxy Proxy `yaml:"proxy,omitempty"`
  120. // Compatibility is used for configurations of working with older or deprecated features.
  121. Compatibility struct {
  122. // Schema1 configures how schema1 manifests will be handled
  123. Schema1 struct {
  124. // TrustKey is the signing key to use for adding the signature to
  125. // schema1 manifests.
  126. TrustKey string `yaml:"signingkeyfile,omitempty"`
  127. // DisableSignatureStore will cause all signatures attached to schema1 manifests
  128. // to be ignored. Signatures will be generated on all schema1 manifest requests
  129. // rather than only requests which converted schema2 to schema1.
  130. DisableSignatureStore bool `yaml:"disablesignaturestore,omitempty"`
  131. } `yaml:"schema1,omitempty"`
  132. } `yaml:"compatibility,omitempty"`
  133. }
  134. // LogHook is composed of hook Level and Type.
  135. // After hooks configuration, it can execute the next handling automatically,
  136. // when defined levels of log message emitted.
  137. // Example: hook can sending an email notification when error log happens in app.
  138. type LogHook struct {
  139. // Disable lets user select to enable hook or not.
  140. Disabled bool `yaml:"disabled,omitempty"`
  141. // Type allows user to select which type of hook handler they want.
  142. Type string `yaml:"type,omitempty"`
  143. // Levels set which levels of log message will let hook executed.
  144. Levels []string `yaml:"levels,omitempty"`
  145. // MailOptions allows user to configurate email parameters.
  146. MailOptions MailOptions `yaml:"options,omitempty"`
  147. }
  148. // MailOptions provides the configuration sections to user, for specific handler.
  149. type MailOptions struct {
  150. SMTP struct {
  151. // Addr defines smtp host address
  152. Addr string `yaml:"addr,omitempty"`
  153. // Username defines user name to smtp host
  154. Username string `yaml:"username,omitempty"`
  155. // Password defines password of login user
  156. Password string `yaml:"password,omitempty"`
  157. // Insecure defines if smtp login skips the secure cerification.
  158. Insecure bool `yaml:"insecure,omitempty"`
  159. } `yaml:"smtp,omitempty"`
  160. // From defines mail sending address
  161. From string `yaml:"from,omitempty"`
  162. // To defines mail receiving address
  163. To []string `yaml:"to,omitempty"`
  164. }
  165. // FileChecker is a type of entry in the health section for checking files.
  166. type FileChecker struct {
  167. // Interval is the duration in between checks
  168. Interval time.Duration `yaml:"interval,omitempty"`
  169. // File is the path to check
  170. File string `yaml:"file,omitempty"`
  171. // Threshold is the number of times a check must fail to trigger an
  172. // unhealthy state
  173. Threshold int `yaml:"threshold,omitempty"`
  174. }
  175. // HTTPChecker is a type of entry in the health section for checking HTTP URIs.
  176. type HTTPChecker struct {
  177. // Timeout is the duration to wait before timing out the HTTP request
  178. Timeout time.Duration `yaml:"interval,omitempty"`
  179. // StatusCode is the expected status code
  180. StatusCode int
  181. // Interval is the duration in between checks
  182. Interval time.Duration `yaml:"interval,omitempty"`
  183. // URI is the HTTP URI to check
  184. URI string `yaml:"uri,omitempty"`
  185. // Headers lists static headers that should be added to all requests
  186. Headers http.Header `yaml:"headers"`
  187. // Threshold is the number of times a check must fail to trigger an
  188. // unhealthy state
  189. Threshold int `yaml:"threshold,omitempty"`
  190. }
  191. // TCPChecker is a type of entry in the health section for checking TCP servers.
  192. type TCPChecker struct {
  193. // Timeout is the duration to wait before timing out the TCP connection
  194. Timeout time.Duration `yaml:"interval,omitempty"`
  195. // Interval is the duration in between checks
  196. Interval time.Duration `yaml:"interval,omitempty"`
  197. // Addr is the TCP address to check
  198. Addr string `yaml:"addr,omitempty"`
  199. // Threshold is the number of times a check must fail to trigger an
  200. // unhealthy state
  201. Threshold int `yaml:"threshold,omitempty"`
  202. }
  203. // Health provides the configuration section for health checks.
  204. type Health struct {
  205. // FileCheckers is a list of paths to check
  206. FileCheckers []FileChecker `yaml:"file,omitempty"`
  207. // HTTPCheckers is a list of URIs to check
  208. HTTPCheckers []HTTPChecker `yaml:"http,omitempty"`
  209. // TCPCheckers is a list of URIs to check
  210. TCPCheckers []TCPChecker `yaml:"tcp,omitempty"`
  211. // StorageDriver configures a health check on the configured storage
  212. // driver
  213. StorageDriver struct {
  214. // Enabled turns on the health check for the storage driver
  215. Enabled bool `yaml:"enabled,omitempty"`
  216. // Interval is the duration in between checks
  217. Interval time.Duration `yaml:"interval,omitempty"`
  218. // Threshold is the number of times a check must fail to trigger an
  219. // unhealthy state
  220. Threshold int `yaml:"threshold,omitempty"`
  221. } `yaml:"storagedriver,omitempty"`
  222. }
  223. // v0_1Configuration is a Version 0.1 Configuration struct
  224. // This is currently aliased to Configuration, as it is the current version
  225. type v0_1Configuration Configuration
  226. // UnmarshalYAML implements the yaml.Unmarshaler interface
  227. // Unmarshals a string of the form X.Y into a Version, validating that X and Y can represent uints
  228. func (version *Version) UnmarshalYAML(unmarshal func(interface{}) error) error {
  229. var versionString string
  230. err := unmarshal(&versionString)
  231. if err != nil {
  232. return err
  233. }
  234. newVersion := Version(versionString)
  235. if _, err := newVersion.major(); err != nil {
  236. return err
  237. }
  238. if _, err := newVersion.minor(); err != nil {
  239. return err
  240. }
  241. *version = newVersion
  242. return nil
  243. }
  244. // CurrentVersion is the most recent Version that can be parsed
  245. var CurrentVersion = MajorMinorVersion(0, 1)
  246. // Loglevel is the level at which operations are logged
  247. // This can be error, warn, info, or debug
  248. type Loglevel string
  249. // UnmarshalYAML implements the yaml.Umarshaler interface
  250. // Unmarshals a string into a Loglevel, lowercasing the string and validating that it represents a
  251. // valid loglevel
  252. func (loglevel *Loglevel) UnmarshalYAML(unmarshal func(interface{}) error) error {
  253. var loglevelString string
  254. err := unmarshal(&loglevelString)
  255. if err != nil {
  256. return err
  257. }
  258. loglevelString = strings.ToLower(loglevelString)
  259. switch loglevelString {
  260. case "error", "warn", "info", "debug":
  261. default:
  262. return fmt.Errorf("Invalid loglevel %s Must be one of [error, warn, info, debug]", loglevelString)
  263. }
  264. *loglevel = Loglevel(loglevelString)
  265. return nil
  266. }
  267. // Parameters defines a key-value parameters mapping
  268. type Parameters map[string]interface{}
  269. // Storage defines the configuration for registry object storage
  270. type Storage map[string]Parameters
  271. // Type returns the storage driver type, such as filesystem or s3
  272. func (storage Storage) Type() string {
  273. var storageType []string
  274. // Return only key in this map
  275. for k := range storage {
  276. switch k {
  277. case "maintenance":
  278. // allow configuration of maintenance
  279. case "cache":
  280. // allow configuration of caching
  281. case "delete":
  282. // allow configuration of delete
  283. case "redirect":
  284. // allow configuration of redirect
  285. default:
  286. storageType = append(storageType, k)
  287. }
  288. }
  289. if len(storageType) > 1 {
  290. panic("multiple storage drivers specified in configuration or environment: " + strings.Join(storageType, ", "))
  291. }
  292. if len(storageType) == 1 {
  293. return storageType[0]
  294. }
  295. return ""
  296. }
  297. // Parameters returns the Parameters map for a Storage configuration
  298. func (storage Storage) Parameters() Parameters {
  299. return storage[storage.Type()]
  300. }
  301. // setParameter changes the parameter at the provided key to the new value
  302. func (storage Storage) setParameter(key string, value interface{}) {
  303. storage[storage.Type()][key] = value
  304. }
  305. // UnmarshalYAML implements the yaml.Unmarshaler interface
  306. // Unmarshals a single item map into a Storage or a string into a Storage type with no parameters
  307. func (storage *Storage) UnmarshalYAML(unmarshal func(interface{}) error) error {
  308. var storageMap map[string]Parameters
  309. err := unmarshal(&storageMap)
  310. if err == nil {
  311. if len(storageMap) > 1 {
  312. types := make([]string, 0, len(storageMap))
  313. for k := range storageMap {
  314. switch k {
  315. case "maintenance":
  316. // allow for configuration of maintenance
  317. case "cache":
  318. // allow configuration of caching
  319. case "delete":
  320. // allow configuration of delete
  321. case "redirect":
  322. // allow configuration of redirect
  323. default:
  324. types = append(types, k)
  325. }
  326. }
  327. if len(types) > 1 {
  328. return fmt.Errorf("Must provide exactly one storage type. Provided: %v", types)
  329. }
  330. }
  331. *storage = storageMap
  332. return nil
  333. }
  334. var storageType string
  335. err = unmarshal(&storageType)
  336. if err == nil {
  337. *storage = Storage{storageType: Parameters{}}
  338. return nil
  339. }
  340. return err
  341. }
  342. // MarshalYAML implements the yaml.Marshaler interface
  343. func (storage Storage) MarshalYAML() (interface{}, error) {
  344. if storage.Parameters() == nil {
  345. return storage.Type(), nil
  346. }
  347. return map[string]Parameters(storage), nil
  348. }
  349. // Auth defines the configuration for registry authorization.
  350. type Auth map[string]Parameters
  351. // Type returns the storage driver type, such as filesystem or s3
  352. func (auth Auth) Type() string {
  353. // Return only key in this map
  354. for k := range auth {
  355. return k
  356. }
  357. return ""
  358. }
  359. // Parameters returns the Parameters map for an Auth configuration
  360. func (auth Auth) Parameters() Parameters {
  361. return auth[auth.Type()]
  362. }
  363. // setParameter changes the parameter at the provided key to the new value
  364. func (auth Auth) setParameter(key string, value interface{}) {
  365. auth[auth.Type()][key] = value
  366. }
  367. // UnmarshalYAML implements the yaml.Unmarshaler interface
  368. // Unmarshals a single item map into a Storage or a string into a Storage type with no parameters
  369. func (auth *Auth) UnmarshalYAML(unmarshal func(interface{}) error) error {
  370. var m map[string]Parameters
  371. err := unmarshal(&m)
  372. if err == nil {
  373. if len(m) > 1 {
  374. types := make([]string, 0, len(m))
  375. for k := range m {
  376. types = append(types, k)
  377. }
  378. // TODO(stevvooe): May want to change this slightly for
  379. // authorization to allow multiple challenges.
  380. return fmt.Errorf("must provide exactly one type. Provided: %v", types)
  381. }
  382. *auth = m
  383. return nil
  384. }
  385. var authType string
  386. err = unmarshal(&authType)
  387. if err == nil {
  388. *auth = Auth{authType: Parameters{}}
  389. return nil
  390. }
  391. return err
  392. }
  393. // MarshalYAML implements the yaml.Marshaler interface
  394. func (auth Auth) MarshalYAML() (interface{}, error) {
  395. if auth.Parameters() == nil {
  396. return auth.Type(), nil
  397. }
  398. return map[string]Parameters(auth), nil
  399. }
  400. // Notifications configures multiple http endpoints.
  401. type Notifications struct {
  402. // Endpoints is a list of http configurations for endpoints that
  403. // respond to webhook notifications. In the future, we may allow other
  404. // kinds of endpoints, such as external queues.
  405. Endpoints []Endpoint `yaml:"endpoints,omitempty"`
  406. }
  407. // Endpoint describes the configuration of an http webhook notification
  408. // endpoint.
  409. type Endpoint struct {
  410. Name string `yaml:"name"` // identifies the endpoint in the registry instance.
  411. Disabled bool `yaml:"disabled"` // disables the endpoint
  412. URL string `yaml:"url"` // post url for the endpoint.
  413. Headers http.Header `yaml:"headers"` // static headers that should be added to all requests
  414. Timeout time.Duration `yaml:"timeout"` // HTTP timeout
  415. Threshold int `yaml:"threshold"` // circuit breaker threshold before backing off on failure
  416. Backoff time.Duration `yaml:"backoff"` // backoff duration
  417. }
  418. // Reporting defines error reporting methods.
  419. type Reporting struct {
  420. // Bugsnag configures error reporting for Bugsnag (bugsnag.com).
  421. Bugsnag BugsnagReporting `yaml:"bugsnag,omitempty"`
  422. // NewRelic configures error reporting for NewRelic (newrelic.com)
  423. NewRelic NewRelicReporting `yaml:"newrelic,omitempty"`
  424. }
  425. // BugsnagReporting configures error reporting for Bugsnag (bugsnag.com).
  426. type BugsnagReporting struct {
  427. // APIKey is the Bugsnag api key.
  428. APIKey string `yaml:"apikey,omitempty"`
  429. // ReleaseStage tracks where the registry is deployed.
  430. // Examples: production, staging, development
  431. ReleaseStage string `yaml:"releasestage,omitempty"`
  432. // Endpoint is used for specifying an enterprise Bugsnag endpoint.
  433. Endpoint string `yaml:"endpoint,omitempty"`
  434. }
  435. // NewRelicReporting configures error reporting for NewRelic (newrelic.com)
  436. type NewRelicReporting struct {
  437. // LicenseKey is the NewRelic user license key
  438. LicenseKey string `yaml:"licensekey,omitempty"`
  439. // Name is the component name of the registry in NewRelic
  440. Name string `yaml:"name,omitempty"`
  441. // Verbose configures debug output to STDOUT
  442. Verbose bool `yaml:"verbose,omitempty"`
  443. }
  444. // Middleware configures named middlewares to be applied at injection points.
  445. type Middleware struct {
  446. // Name the middleware registers itself as
  447. Name string `yaml:"name"`
  448. // Flag to disable middleware easily
  449. Disabled bool `yaml:"disabled,omitempty"`
  450. // Map of parameters that will be passed to the middleware's initialization function
  451. Options Parameters `yaml:"options"`
  452. }
  453. // Proxy configures the registry as a pull through cache
  454. type Proxy struct {
  455. // RemoteURL is the URL of the remote registry
  456. RemoteURL string `yaml:"remoteurl"`
  457. // Username of the hub user
  458. Username string `yaml:"username"`
  459. // Password of the hub user
  460. Password string `yaml:"password"`
  461. }
  462. // Parse parses an input configuration yaml document into a Configuration struct
  463. // This should generally be capable of handling old configuration format versions
  464. //
  465. // Environment variables may be used to override configuration parameters other than version,
  466. // following the scheme below:
  467. // Configuration.Abc may be replaced by the value of REGISTRY_ABC,
  468. // Configuration.Abc.Xyz may be replaced by the value of REGISTRY_ABC_XYZ, and so forth
  469. func Parse(rd io.Reader) (*Configuration, error) {
  470. in, err := ioutil.ReadAll(rd)
  471. if err != nil {
  472. return nil, err
  473. }
  474. p := NewParser("registry", []VersionedParseInfo{
  475. {
  476. Version: MajorMinorVersion(0, 1),
  477. ParseAs: reflect.TypeOf(v0_1Configuration{}),
  478. ConversionFunc: func(c interface{}) (interface{}, error) {
  479. if v0_1, ok := c.(*v0_1Configuration); ok {
  480. if v0_1.Loglevel == Loglevel("") {
  481. v0_1.Loglevel = Loglevel("info")
  482. }
  483. if v0_1.Storage.Type() == "" {
  484. return nil, fmt.Errorf("No storage configuration provided")
  485. }
  486. return (*Configuration)(v0_1), nil
  487. }
  488. return nil, fmt.Errorf("Expected *v0_1Configuration, received %#v", c)
  489. },
  490. },
  491. })
  492. config := new(Configuration)
  493. err = p.Parse(in, config)
  494. if err != nil {
  495. return nil, err
  496. }
  497. return config, nil
  498. }