config.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. package config
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "math"
  7. "os"
  8. "regexp"
  9. "runtime"
  10. log "github.com/sirupsen/logrus"
  11. "github.com/imgproxy/imgproxy/v3/config/configurators"
  12. "github.com/imgproxy/imgproxy/v3/imagetype"
  13. "github.com/imgproxy/imgproxy/v3/version"
  14. )
  15. type URLReplacement = configurators.URLReplacement
  16. var (
  17. Network string
  18. Bind string
  19. ReadTimeout int
  20. WriteTimeout int
  21. KeepAliveTimeout int
  22. ClientKeepAliveTimeout int
  23. DownloadTimeout int
  24. Workers int
  25. RequestsQueueSize int
  26. MaxClients int
  27. TTL int
  28. CacheControlPassthrough bool
  29. SetCanonicalHeader bool
  30. SoReuseport bool
  31. PathPrefix string
  32. MaxSrcResolution int
  33. MaxSrcFileSize int
  34. MaxAnimationFrames int
  35. MaxAnimationFrameResolution int
  36. MaxSvgCheckBytes int
  37. MaxRedirects int
  38. AllowSecurityOptions bool
  39. JpegProgressive bool
  40. PngInterlaced bool
  41. PngQuantize bool
  42. PngQuantizationColors int
  43. AvifSpeed int
  44. Quality int
  45. FormatQuality map[imagetype.Type]int
  46. StripMetadata bool
  47. KeepCopyright bool
  48. StripColorProfile bool
  49. AutoRotate bool
  50. EnforceThumbnail bool
  51. ReturnAttachment bool
  52. SvgFixUnsupported bool
  53. EnableWebpDetection bool
  54. EnforceWebp bool
  55. EnableAvifDetection bool
  56. EnforceAvif bool
  57. EnableClientHints bool
  58. PreferredFormats []imagetype.Type
  59. SkipProcessingFormats []imagetype.Type
  60. UseLinearColorspace bool
  61. DisableShrinkOnLoad bool
  62. Keys [][]byte
  63. Salts [][]byte
  64. SignatureSize int
  65. Secret string
  66. AllowOrigin string
  67. UserAgent string
  68. IgnoreSslVerification bool
  69. DevelopmentErrorsMode bool
  70. AllowedSources []*regexp.Regexp
  71. AllowLoopbackSourceAddresses bool
  72. AllowLinkLocalSourceAddresses bool
  73. AllowPrivateSourceAddresses bool
  74. SanitizeSvg bool
  75. CookiePassthrough bool
  76. CookieBaseURL string
  77. LocalFileSystemRoot string
  78. S3Enabled bool
  79. S3Region string
  80. S3Endpoint string
  81. S3AssumeRoleArn string
  82. S3MultiRegion bool
  83. S3DecryptionClientEnabled bool
  84. GCSEnabled bool
  85. GCSKey string
  86. GCSEndpoint string
  87. ABSEnabled bool
  88. ABSName string
  89. ABSKey string
  90. ABSEndpoint string
  91. SwiftEnabled bool
  92. SwiftUsername string
  93. SwiftAPIKey string
  94. SwiftAuthURL string
  95. SwiftDomain string
  96. SwiftTenant string
  97. SwiftAuthVersion int
  98. SwiftConnectTimeoutSeconds int
  99. SwiftTimeoutSeconds int
  100. ETagEnabled bool
  101. ETagBuster string
  102. LastModifiedEnabled bool
  103. BaseURL string
  104. URLReplacements []URLReplacement
  105. Presets []string
  106. OnlyPresets bool
  107. WatermarkData string
  108. WatermarkPath string
  109. WatermarkURL string
  110. WatermarkOpacity float64
  111. FallbackImageData string
  112. FallbackImagePath string
  113. FallbackImageURL string
  114. FallbackImageHTTPCode int
  115. FallbackImageTTL int
  116. DataDogEnable bool
  117. DataDogEnableMetrics bool
  118. NewRelicAppName string
  119. NewRelicKey string
  120. NewRelicLabels map[string]string
  121. PrometheusBind string
  122. PrometheusNamespace string
  123. OpenTelemetryEndpoint string
  124. OpenTelemetryProtocol string
  125. OpenTelemetryServiceName string
  126. OpenTelemetryEnableMetrics bool
  127. OpenTelemetryServerCert string
  128. OpenTelemetryClientCert string
  129. OpenTelemetryClientKey string
  130. OpenTelemetryGRPCInsecure bool
  131. OpenTelemetryPropagators []string
  132. OpenTelemetryTraceIDGenerator string
  133. OpenTelemetryConnectionTimeout int
  134. CloudWatchServiceName string
  135. CloudWatchNamespace string
  136. CloudWatchRegion string
  137. BugsnagKey string
  138. BugsnagStage string
  139. HoneybadgerKey string
  140. HoneybadgerEnv string
  141. SentryDSN string
  142. SentryEnvironment string
  143. SentryRelease string
  144. AirbrakeProjecID int
  145. AirbrakeProjecKey string
  146. AirbrakeEnv string
  147. ReportDownloadingErrors bool
  148. EnableDebugHeaders bool
  149. FreeMemoryInterval int
  150. DownloadBufferSize int
  151. BufferPoolCalibrationThreshold int
  152. HealthCheckPath string
  153. )
  154. var (
  155. keyPath string
  156. saltPath string
  157. presetsPath string
  158. )
  159. func init() {
  160. Reset()
  161. flag.StringVar(&keyPath, "keypath", "", "path of the file with hex-encoded key")
  162. flag.StringVar(&saltPath, "saltpath", "", "path of the file with hex-encoded salt")
  163. flag.StringVar(&presetsPath, "presets", "", "path of the file with presets")
  164. }
  165. func Reset() {
  166. Network = "tcp"
  167. Bind = ":8080"
  168. ReadTimeout = 10
  169. WriteTimeout = 10
  170. KeepAliveTimeout = 10
  171. ClientKeepAliveTimeout = 90
  172. DownloadTimeout = 5
  173. Workers = runtime.GOMAXPROCS(0) * 2
  174. RequestsQueueSize = 0
  175. MaxClients = 2048
  176. TTL = 31536000
  177. CacheControlPassthrough = false
  178. SetCanonicalHeader = false
  179. SoReuseport = false
  180. PathPrefix = ""
  181. MaxSrcResolution = 16800000
  182. MaxSrcFileSize = 0
  183. MaxAnimationFrames = 1
  184. MaxAnimationFrameResolution = 0
  185. MaxSvgCheckBytes = 32 * 1024
  186. MaxRedirects = 10
  187. AllowSecurityOptions = false
  188. JpegProgressive = false
  189. PngInterlaced = false
  190. PngQuantize = false
  191. PngQuantizationColors = 256
  192. AvifSpeed = 9
  193. Quality = 80
  194. FormatQuality = map[imagetype.Type]int{imagetype.AVIF: 65}
  195. StripMetadata = true
  196. KeepCopyright = true
  197. StripColorProfile = true
  198. AutoRotate = true
  199. EnforceThumbnail = false
  200. ReturnAttachment = false
  201. SvgFixUnsupported = false
  202. EnableWebpDetection = false
  203. EnforceWebp = false
  204. EnableAvifDetection = false
  205. EnforceAvif = false
  206. EnableClientHints = false
  207. PreferredFormats = []imagetype.Type{
  208. imagetype.JPEG,
  209. imagetype.PNG,
  210. imagetype.GIF,
  211. }
  212. SkipProcessingFormats = make([]imagetype.Type, 0)
  213. UseLinearColorspace = false
  214. DisableShrinkOnLoad = false
  215. Keys = make([][]byte, 0)
  216. Salts = make([][]byte, 0)
  217. SignatureSize = 32
  218. Secret = ""
  219. AllowOrigin = ""
  220. UserAgent = fmt.Sprintf("imgproxy/%s", version.Version())
  221. IgnoreSslVerification = false
  222. DevelopmentErrorsMode = false
  223. AllowedSources = make([]*regexp.Regexp, 0)
  224. AllowLoopbackSourceAddresses = false
  225. AllowLinkLocalSourceAddresses = false
  226. AllowPrivateSourceAddresses = true
  227. SanitizeSvg = true
  228. CookiePassthrough = false
  229. CookieBaseURL = ""
  230. LocalFileSystemRoot = ""
  231. S3Enabled = false
  232. S3Region = ""
  233. S3Endpoint = ""
  234. S3AssumeRoleArn = ""
  235. S3MultiRegion = false
  236. S3DecryptionClientEnabled = false
  237. GCSEnabled = false
  238. GCSKey = ""
  239. ABSEnabled = false
  240. ABSName = ""
  241. ABSKey = ""
  242. ABSEndpoint = ""
  243. SwiftEnabled = false
  244. SwiftUsername = ""
  245. SwiftAPIKey = ""
  246. SwiftAuthURL = ""
  247. SwiftAuthVersion = 0
  248. SwiftTenant = ""
  249. SwiftDomain = ""
  250. SwiftConnectTimeoutSeconds = 10
  251. SwiftTimeoutSeconds = 60
  252. ETagEnabled = false
  253. ETagBuster = ""
  254. LastModifiedEnabled = false
  255. BaseURL = ""
  256. URLReplacements = make([]URLReplacement, 0)
  257. Presets = make([]string, 0)
  258. OnlyPresets = false
  259. WatermarkData = ""
  260. WatermarkPath = ""
  261. WatermarkURL = ""
  262. WatermarkOpacity = 1
  263. FallbackImageData = ""
  264. FallbackImagePath = ""
  265. FallbackImageURL = ""
  266. FallbackImageHTTPCode = 200
  267. FallbackImageTTL = 0
  268. DataDogEnable = false
  269. NewRelicAppName = ""
  270. NewRelicKey = ""
  271. NewRelicLabels = make(map[string]string)
  272. PrometheusBind = ""
  273. PrometheusNamespace = ""
  274. OpenTelemetryEndpoint = ""
  275. OpenTelemetryProtocol = "grpc"
  276. OpenTelemetryServiceName = "imgproxy"
  277. OpenTelemetryEnableMetrics = false
  278. OpenTelemetryServerCert = ""
  279. OpenTelemetryClientCert = ""
  280. OpenTelemetryClientKey = ""
  281. OpenTelemetryGRPCInsecure = true
  282. OpenTelemetryPropagators = make([]string, 0)
  283. OpenTelemetryTraceIDGenerator = "xray"
  284. OpenTelemetryConnectionTimeout = 5
  285. CloudWatchServiceName = ""
  286. CloudWatchNamespace = "imgproxy"
  287. CloudWatchRegion = ""
  288. BugsnagKey = ""
  289. BugsnagStage = "production"
  290. HoneybadgerKey = ""
  291. HoneybadgerEnv = "production"
  292. SentryDSN = ""
  293. SentryEnvironment = "production"
  294. SentryRelease = fmt.Sprintf("imgproxy@%s", version.Version())
  295. AirbrakeProjecID = 0
  296. AirbrakeProjecKey = ""
  297. AirbrakeEnv = "production"
  298. ReportDownloadingErrors = true
  299. EnableDebugHeaders = false
  300. FreeMemoryInterval = 10
  301. DownloadBufferSize = 0
  302. BufferPoolCalibrationThreshold = 1024
  303. HealthCheckPath = ""
  304. }
  305. func Configure() error {
  306. if port := os.Getenv("PORT"); len(port) > 0 {
  307. Bind = fmt.Sprintf(":%s", port)
  308. }
  309. configurators.String(&Network, "IMGPROXY_NETWORK")
  310. configurators.String(&Bind, "IMGPROXY_BIND")
  311. configurators.Int(&ReadTimeout, "IMGPROXY_READ_TIMEOUT")
  312. configurators.Int(&WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
  313. configurators.Int(&KeepAliveTimeout, "IMGPROXY_KEEP_ALIVE_TIMEOUT")
  314. configurators.Int(&ClientKeepAliveTimeout, "IMGPROXY_CLIENT_KEEP_ALIVE_TIMEOUT")
  315. configurators.Int(&DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
  316. configurators.Int(&Workers, "IMGPROXY_CONCURRENCY")
  317. configurators.Int(&Workers, "IMGPROXY_WORKERS")
  318. configurators.Int(&RequestsQueueSize, "IMGPROXY_REQUESTS_QUEUE_SIZE")
  319. configurators.Int(&MaxClients, "IMGPROXY_MAX_CLIENTS")
  320. configurators.Int(&TTL, "IMGPROXY_TTL")
  321. configurators.Bool(&CacheControlPassthrough, "IMGPROXY_CACHE_CONTROL_PASSTHROUGH")
  322. configurators.Bool(&SetCanonicalHeader, "IMGPROXY_SET_CANONICAL_HEADER")
  323. configurators.Bool(&SoReuseport, "IMGPROXY_SO_REUSEPORT")
  324. configurators.URLPath(&PathPrefix, "IMGPROXY_PATH_PREFIX")
  325. configurators.MegaInt(&MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
  326. configurators.Int(&MaxSrcFileSize, "IMGPROXY_MAX_SRC_FILE_SIZE")
  327. configurators.Int(&MaxSvgCheckBytes, "IMGPROXY_MAX_SVG_CHECK_BYTES")
  328. configurators.Int(&MaxAnimationFrames, "IMGPROXY_MAX_ANIMATION_FRAMES")
  329. configurators.MegaInt(&MaxAnimationFrameResolution, "IMGPROXY_MAX_ANIMATION_FRAME_RESOLUTION")
  330. configurators.Int(&MaxRedirects, "IMGPROXY_MAX_REDIRECTS")
  331. configurators.Patterns(&AllowedSources, "IMGPROXY_ALLOWED_SOURCES")
  332. configurators.Bool(&AllowLoopbackSourceAddresses, "IMGPROXY_ALLOW_LOOPBACK_SOURCE_ADDRESSES")
  333. configurators.Bool(&AllowLinkLocalSourceAddresses, "IMGPROXY_ALLOW_LINK_LOCAL_SOURCE_ADDRESSES")
  334. configurators.Bool(&AllowPrivateSourceAddresses, "IMGPROXY_ALLOW_PRIVATE_SOURCE_ADDRESSES")
  335. configurators.Bool(&SanitizeSvg, "IMGPROXY_SANITIZE_SVG")
  336. configurators.Bool(&AllowSecurityOptions, "IMGPROXY_ALLOW_SECURITY_OPTIONS")
  337. configurators.Bool(&JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE")
  338. configurators.Bool(&PngInterlaced, "IMGPROXY_PNG_INTERLACED")
  339. configurators.Bool(&PngQuantize, "IMGPROXY_PNG_QUANTIZE")
  340. configurators.Int(&PngQuantizationColors, "IMGPROXY_PNG_QUANTIZATION_COLORS")
  341. configurators.Int(&AvifSpeed, "IMGPROXY_AVIF_SPEED")
  342. configurators.Int(&Quality, "IMGPROXY_QUALITY")
  343. if err := configurators.ImageTypesQuality(FormatQuality, "IMGPROXY_FORMAT_QUALITY"); err != nil {
  344. return err
  345. }
  346. configurators.Bool(&StripMetadata, "IMGPROXY_STRIP_METADATA")
  347. configurators.Bool(&KeepCopyright, "IMGPROXY_KEEP_COPYRIGHT")
  348. configurators.Bool(&StripColorProfile, "IMGPROXY_STRIP_COLOR_PROFILE")
  349. configurators.Bool(&AutoRotate, "IMGPROXY_AUTO_ROTATE")
  350. configurators.Bool(&EnforceThumbnail, "IMGPROXY_ENFORCE_THUMBNAIL")
  351. configurators.Bool(&ReturnAttachment, "IMGPROXY_RETURN_ATTACHMENT")
  352. configurators.Bool(&SvgFixUnsupported, "IMGPROXY_SVG_FIX_UNSUPPORTED")
  353. configurators.Bool(&EnableWebpDetection, "IMGPROXY_ENABLE_WEBP_DETECTION")
  354. configurators.Bool(&EnforceWebp, "IMGPROXY_ENFORCE_WEBP")
  355. configurators.Bool(&EnableAvifDetection, "IMGPROXY_ENABLE_AVIF_DETECTION")
  356. configurators.Bool(&EnforceAvif, "IMGPROXY_ENFORCE_AVIF")
  357. configurators.Bool(&EnableClientHints, "IMGPROXY_ENABLE_CLIENT_HINTS")
  358. configurators.URLPath(&HealthCheckPath, "IMGPROXY_HEALTH_CHECK_PATH")
  359. if err := configurators.ImageTypes(&PreferredFormats, "IMGPROXY_PREFERRED_FORMATS"); err != nil {
  360. return err
  361. }
  362. if err := configurators.ImageTypes(&SkipProcessingFormats, "IMGPROXY_SKIP_PROCESSING_FORMATS"); err != nil {
  363. return err
  364. }
  365. configurators.Bool(&UseLinearColorspace, "IMGPROXY_USE_LINEAR_COLORSPACE")
  366. configurators.Bool(&DisableShrinkOnLoad, "IMGPROXY_DISABLE_SHRINK_ON_LOAD")
  367. if err := configurators.HexSlice(&Keys, "IMGPROXY_KEY"); err != nil {
  368. return err
  369. }
  370. if err := configurators.HexSlice(&Salts, "IMGPROXY_SALT"); err != nil {
  371. return err
  372. }
  373. configurators.Int(&SignatureSize, "IMGPROXY_SIGNATURE_SIZE")
  374. if err := configurators.HexSliceFile(&Keys, keyPath); err != nil {
  375. return err
  376. }
  377. if err := configurators.HexSliceFile(&Salts, saltPath); err != nil {
  378. return err
  379. }
  380. configurators.String(&Secret, "IMGPROXY_SECRET")
  381. configurators.String(&AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
  382. configurators.String(&UserAgent, "IMGPROXY_USER_AGENT")
  383. configurators.Bool(&IgnoreSslVerification, "IMGPROXY_IGNORE_SSL_VERIFICATION")
  384. configurators.Bool(&DevelopmentErrorsMode, "IMGPROXY_DEVELOPMENT_ERRORS_MODE")
  385. configurators.Bool(&CookiePassthrough, "IMGPROXY_COOKIE_PASSTHROUGH")
  386. configurators.String(&CookieBaseURL, "IMGPROXY_COOKIE_BASE_URL")
  387. configurators.String(&LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
  388. configurators.Bool(&S3Enabled, "IMGPROXY_USE_S3")
  389. configurators.String(&S3Region, "IMGPROXY_S3_REGION")
  390. configurators.String(&S3Endpoint, "IMGPROXY_S3_ENDPOINT")
  391. configurators.String(&S3AssumeRoleArn, "IMGPROXY_S3_ASSUME_ROLE_ARN")
  392. configurators.Bool(&S3MultiRegion, "IMGPROXY_S3_MULTI_REGION")
  393. configurators.Bool(&S3DecryptionClientEnabled, "IMGPROXY_S3_USE_DECRYPTION_CLIENT")
  394. configurators.Bool(&GCSEnabled, "IMGPROXY_USE_GCS")
  395. configurators.String(&GCSKey, "IMGPROXY_GCS_KEY")
  396. configurators.String(&GCSEndpoint, "IMGPROXY_GCS_ENDPOINT")
  397. configurators.Bool(&ABSEnabled, "IMGPROXY_USE_ABS")
  398. configurators.String(&ABSName, "IMGPROXY_ABS_NAME")
  399. configurators.String(&ABSKey, "IMGPROXY_ABS_KEY")
  400. configurators.String(&ABSEndpoint, "IMGPROXY_ABS_ENDPOINT")
  401. configurators.Bool(&SwiftEnabled, "IMGPROXY_USE_SWIFT")
  402. configurators.String(&SwiftUsername, "IMGPROXY_SWIFT_USERNAME")
  403. configurators.String(&SwiftAPIKey, "IMGPROXY_SWIFT_API_KEY")
  404. configurators.String(&SwiftAuthURL, "IMGPROXY_SWIFT_AUTH_URL")
  405. configurators.String(&SwiftDomain, "IMGPROXY_SWIFT_DOMAIN")
  406. configurators.String(&SwiftTenant, "IMGPROXY_SWIFT_TENANT")
  407. configurators.Int(&SwiftConnectTimeoutSeconds, "IMGPROXY_SWIFT_CONNECT_TIMEOUT_SECONDS")
  408. configurators.Int(&SwiftTimeoutSeconds, "IMGPROXY_SWIFT_TIMEOUT_SECONDS")
  409. configurators.Bool(&ETagEnabled, "IMGPROXY_USE_ETAG")
  410. configurators.String(&ETagBuster, "IMGPROXY_ETAG_BUSTER")
  411. configurators.Bool(&LastModifiedEnabled, "IMGPROXY_USE_LAST_MODIFIED")
  412. configurators.String(&BaseURL, "IMGPROXY_BASE_URL")
  413. if err := configurators.Replacements(&URLReplacements, "IMGPROXY_URL_REPLACEMENTS"); err != nil {
  414. return err
  415. }
  416. configurators.StringSlice(&Presets, "IMGPROXY_PRESETS")
  417. if err := configurators.StringSliceFile(&Presets, presetsPath); err != nil {
  418. return err
  419. }
  420. configurators.Bool(&OnlyPresets, "IMGPROXY_ONLY_PRESETS")
  421. configurators.String(&WatermarkData, "IMGPROXY_WATERMARK_DATA")
  422. configurators.String(&WatermarkPath, "IMGPROXY_WATERMARK_PATH")
  423. configurators.String(&WatermarkURL, "IMGPROXY_WATERMARK_URL")
  424. configurators.Float(&WatermarkOpacity, "IMGPROXY_WATERMARK_OPACITY")
  425. configurators.String(&FallbackImageData, "IMGPROXY_FALLBACK_IMAGE_DATA")
  426. configurators.String(&FallbackImagePath, "IMGPROXY_FALLBACK_IMAGE_PATH")
  427. configurators.String(&FallbackImageURL, "IMGPROXY_FALLBACK_IMAGE_URL")
  428. configurators.Int(&FallbackImageHTTPCode, "IMGPROXY_FALLBACK_IMAGE_HTTP_CODE")
  429. configurators.Int(&FallbackImageTTL, "IMGPROXY_FALLBACK_IMAGE_TTL")
  430. configurators.Bool(&DataDogEnable, "IMGPROXY_DATADOG_ENABLE")
  431. configurators.Bool(&DataDogEnableMetrics, "IMGPROXY_DATADOG_ENABLE_ADDITIONAL_METRICS")
  432. configurators.String(&NewRelicAppName, "IMGPROXY_NEW_RELIC_APP_NAME")
  433. configurators.String(&NewRelicKey, "IMGPROXY_NEW_RELIC_KEY")
  434. configurators.StringMap(&NewRelicLabels, "IMGPROXY_NEW_RELIC_LABELS")
  435. configurators.String(&PrometheusBind, "IMGPROXY_PROMETHEUS_BIND")
  436. configurators.String(&PrometheusNamespace, "IMGPROXY_PROMETHEUS_NAMESPACE")
  437. configurators.String(&OpenTelemetryEndpoint, "IMGPROXY_OPEN_TELEMETRY_ENDPOINT")
  438. configurators.String(&OpenTelemetryProtocol, "IMGPROXY_OPEN_TELEMETRY_PROTOCOL")
  439. configurators.String(&OpenTelemetryServiceName, "IMGPROXY_OPEN_TELEMETRY_SERVICE_NAME")
  440. configurators.Bool(&OpenTelemetryEnableMetrics, "IMGPROXY_OPEN_TELEMETRY_ENABLE_METRICS")
  441. configurators.String(&OpenTelemetryServerCert, "IMGPROXY_OPEN_TELEMETRY_SERVER_CERT")
  442. configurators.String(&OpenTelemetryClientCert, "IMGPROXY_OPEN_TELEMETRY_CLIENT_CERT")
  443. configurators.String(&OpenTelemetryClientKey, "IMGPROXY_OPEN_TELEMETRY_CLIENT_KEY")
  444. configurators.Bool(&OpenTelemetryGRPCInsecure, "IMGPROXY_OPEN_TELEMETRY_GRPC_INSECURE")
  445. configurators.StringSlice(&OpenTelemetryPropagators, "IMGPROXY_OPEN_TELEMETRY_PROPAGATORS")
  446. configurators.String(&OpenTelemetryTraceIDGenerator, "IMGPROXY_OPEN_TELEMETRY_TRACE_ID_GENERATOR")
  447. configurators.Int(&OpenTelemetryConnectionTimeout, "IMGPROXY_OPEN_TELEMETRY_CONNECTION_TIMEOUT")
  448. configurators.String(&CloudWatchServiceName, "IMGPROXY_CLOUD_WATCH_SERVICE_NAME")
  449. configurators.String(&CloudWatchNamespace, "IMGPROXY_CLOUD_WATCH_NAMESPACE")
  450. configurators.String(&CloudWatchRegion, "IMGPROXY_CLOUD_WATCH_REGION")
  451. configurators.String(&BugsnagKey, "IMGPROXY_BUGSNAG_KEY")
  452. configurators.String(&BugsnagStage, "IMGPROXY_BUGSNAG_STAGE")
  453. configurators.String(&HoneybadgerKey, "IMGPROXY_HONEYBADGER_KEY")
  454. configurators.String(&HoneybadgerEnv, "IMGPROXY_HONEYBADGER_ENV")
  455. configurators.String(&SentryDSN, "IMGPROXY_SENTRY_DSN")
  456. configurators.String(&SentryEnvironment, "IMGPROXY_SENTRY_ENVIRONMENT")
  457. configurators.String(&SentryRelease, "IMGPROXY_SENTRY_RELEASE")
  458. configurators.Int(&AirbrakeProjecID, "IMGPROXY_AIRBRAKE_PROJECT_ID")
  459. configurators.String(&AirbrakeProjecKey, "IMGPROXY_AIRBRAKE_PROJECT_KEY")
  460. configurators.String(&AirbrakeEnv, "IMGPROXY_AIRBRAKE_ENVIRONMENT")
  461. configurators.Bool(&ReportDownloadingErrors, "IMGPROXY_REPORT_DOWNLOADING_ERRORS")
  462. configurators.Bool(&EnableDebugHeaders, "IMGPROXY_ENABLE_DEBUG_HEADERS")
  463. configurators.Int(&FreeMemoryInterval, "IMGPROXY_FREE_MEMORY_INTERVAL")
  464. configurators.Int(&DownloadBufferSize, "IMGPROXY_DOWNLOAD_BUFFER_SIZE")
  465. configurators.Int(&BufferPoolCalibrationThreshold, "IMGPROXY_BUFFER_POOL_CALIBRATION_THRESHOLD")
  466. if len(Keys) != len(Salts) {
  467. return fmt.Errorf("Number of keys and number of salts should be equal. Keys: %d, salts: %d", len(Keys), len(Salts))
  468. }
  469. if len(Keys) == 0 {
  470. log.Warning("No keys defined, so signature checking is disabled")
  471. }
  472. if len(Salts) == 0 {
  473. log.Warning("No salts defined, so signature checking is disabled")
  474. }
  475. if SignatureSize < 1 || SignatureSize > 32 {
  476. return fmt.Errorf("Signature size should be within 1 and 32, now - %d\n", SignatureSize)
  477. }
  478. if len(Bind) == 0 {
  479. return errors.New("Bind address is not defined")
  480. }
  481. if ReadTimeout <= 0 {
  482. return fmt.Errorf("Read timeout should be greater than 0, now - %d\n", ReadTimeout)
  483. }
  484. if WriteTimeout <= 0 {
  485. return fmt.Errorf("Write timeout should be greater than 0, now - %d\n", WriteTimeout)
  486. }
  487. if KeepAliveTimeout < 0 {
  488. return fmt.Errorf("KeepAlive timeout should be greater than or equal to 0, now - %d\n", KeepAliveTimeout)
  489. }
  490. if ClientKeepAliveTimeout < 0 {
  491. return fmt.Errorf("Client KeepAlive timeout should be greater than or equal to 0, now - %d\n", ClientKeepAliveTimeout)
  492. }
  493. if DownloadTimeout <= 0 {
  494. return fmt.Errorf("Download timeout should be greater than 0, now - %d\n", DownloadTimeout)
  495. }
  496. if Workers <= 0 {
  497. return fmt.Errorf("Workers number should be greater than 0, now - %d\n", Workers)
  498. }
  499. if RequestsQueueSize < 0 {
  500. return fmt.Errorf("Requests queue size should be greater than or equal 0, now - %d\n", RequestsQueueSize)
  501. }
  502. if MaxClients < 0 {
  503. return fmt.Errorf("Max clients number should be greater than or equal 0, now - %d\n", MaxClients)
  504. }
  505. if TTL < 0 {
  506. return fmt.Errorf("TTL should be greater than or equal to 0, now - %d\n", TTL)
  507. }
  508. if MaxSrcResolution <= 0 {
  509. return fmt.Errorf("Max src resolution should be greater than 0, now - %d\n", MaxSrcResolution)
  510. }
  511. if MaxSrcFileSize < 0 {
  512. return fmt.Errorf("Max src file size should be greater than or equal to 0, now - %d\n", MaxSrcFileSize)
  513. }
  514. if MaxAnimationFrames <= 0 {
  515. return fmt.Errorf("Max animation frames should be greater than 0, now - %d\n", MaxAnimationFrames)
  516. }
  517. if PngQuantizationColors < 2 {
  518. return fmt.Errorf("Png quantization colors should be greater than 1, now - %d\n", PngQuantizationColors)
  519. } else if PngQuantizationColors > 256 {
  520. return fmt.Errorf("Png quantization colors can't be greater than 256, now - %d\n", PngQuantizationColors)
  521. }
  522. if AvifSpeed < 0 {
  523. return fmt.Errorf("Avif speed should be greater than 0, now - %d\n", AvifSpeed)
  524. } else if AvifSpeed > 9 {
  525. return fmt.Errorf("Avif speed can't be greater than 9, now - %d\n", AvifSpeed)
  526. }
  527. if Quality <= 0 {
  528. return fmt.Errorf("Quality should be greater than 0, now - %d\n", Quality)
  529. } else if Quality > 100 {
  530. return fmt.Errorf("Quality can't be greater than 100, now - %d\n", Quality)
  531. }
  532. if len(PreferredFormats) == 0 {
  533. return errors.New("At least one preferred format should be specified")
  534. }
  535. if IgnoreSslVerification {
  536. log.Warning("Ignoring SSL verification is very unsafe")
  537. }
  538. if LocalFileSystemRoot != "" {
  539. stat, err := os.Stat(LocalFileSystemRoot)
  540. if err != nil {
  541. return fmt.Errorf("Cannot use local directory: %s", err)
  542. }
  543. if !stat.IsDir() {
  544. return errors.New("Cannot use local directory: not a directory")
  545. }
  546. if LocalFileSystemRoot == "/" {
  547. log.Warning("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
  548. }
  549. }
  550. if _, ok := os.LookupEnv("IMGPROXY_USE_GCS"); !ok && len(GCSKey) > 0 {
  551. log.Warning("Set IMGPROXY_USE_GCS to true since it may be required by future versions to enable GCS support")
  552. GCSEnabled = true
  553. }
  554. if WatermarkOpacity <= 0 {
  555. return errors.New("Watermark opacity should be greater than 0")
  556. } else if WatermarkOpacity > 1 {
  557. return errors.New("Watermark opacity should be less than or equal to 1")
  558. }
  559. if FallbackImageTTL < 0 {
  560. return fmt.Errorf("Fallback image TTL should be greater than or equal to 0, now - %d\n", TTL)
  561. }
  562. if FallbackImageHTTPCode < 100 || FallbackImageHTTPCode > 599 {
  563. return errors.New("Fallback image HTTP code should be between 100 and 599")
  564. }
  565. if len(PrometheusBind) > 0 && PrometheusBind == Bind {
  566. return errors.New("Can't use the same binding for the main server and Prometheus")
  567. }
  568. if OpenTelemetryConnectionTimeout < 1 {
  569. return errors.New("OpenTelemetry connection timeout should be greater than zero")
  570. }
  571. if FreeMemoryInterval <= 0 {
  572. return errors.New("Free memory interval should be greater than zero")
  573. }
  574. if DownloadBufferSize < 0 {
  575. return errors.New("Download buffer size should be greater than or equal to 0")
  576. } else if DownloadBufferSize > math.MaxInt32 {
  577. return fmt.Errorf("Download buffer size can't be greater than %d", math.MaxInt32)
  578. }
  579. if BufferPoolCalibrationThreshold < 64 {
  580. return errors.New("Buffer pool calibration threshold should be greater than or equal to 64")
  581. }
  582. return nil
  583. }