|
| 1 | +package encoding |
| 2 | + |
| 3 | +import ( |
| 4 | + "compress/gzip" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/open-policy-agent/opa/util" |
| 8 | +) |
| 9 | + |
| 10 | +var defaultGzipMinLength = 1024 |
| 11 | +var defaultGzipCompressionLevel = gzip.BestCompression |
| 12 | + |
| 13 | +// Config represents the configuration for the Server.Encoding settings |
| 14 | +type Config struct { |
| 15 | + Gzip *Gzip `json:"gzip,omitempty"` |
| 16 | +} |
| 17 | + |
| 18 | +// Gzip represents the configuration for the Server.Encoding.Gzip settings |
| 19 | +type Gzip struct { |
| 20 | + MinLength *int `json:"min_length,omitempty"` // the minimum length of a response that will be gzipped |
| 21 | + CompressionLevel *int `json:"compression_level,omitempty"` // the compression level for gzip |
| 22 | +} |
| 23 | + |
| 24 | +// ConfigBuilder assists in the construction of the plugin configuration. |
| 25 | +type ConfigBuilder struct { |
| 26 | + raw []byte |
| 27 | +} |
| 28 | + |
| 29 | +// NewConfigBuilder returns a new ConfigBuilder to build and parse the server config |
| 30 | +func NewConfigBuilder() *ConfigBuilder { |
| 31 | + return &ConfigBuilder{} |
| 32 | +} |
| 33 | + |
| 34 | +// WithBytes sets the raw server config |
| 35 | +func (b *ConfigBuilder) WithBytes(config []byte) *ConfigBuilder { |
| 36 | + b.raw = config |
| 37 | + return b |
| 38 | +} |
| 39 | + |
| 40 | +// Parse returns a valid Config object with defaults injected. |
| 41 | +func (b *ConfigBuilder) Parse() (*Config, error) { |
| 42 | + if b.raw == nil { |
| 43 | + defaultConfig := &Config{ |
| 44 | + Gzip: &Gzip{ |
| 45 | + MinLength: &defaultGzipMinLength, |
| 46 | + CompressionLevel: &defaultGzipCompressionLevel, |
| 47 | + }, |
| 48 | + } |
| 49 | + return defaultConfig, nil |
| 50 | + } |
| 51 | + |
| 52 | + var result Config |
| 53 | + |
| 54 | + if err := util.Unmarshal(b.raw, &result); err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + return &result, result.validateAndInjectDefaults() |
| 59 | +} |
| 60 | + |
| 61 | +func (c *Config) validateAndInjectDefaults() error { |
| 62 | + if c.Gzip == nil { |
| 63 | + c.Gzip = &Gzip{ |
| 64 | + MinLength: &defaultGzipMinLength, |
| 65 | + CompressionLevel: &defaultGzipCompressionLevel, |
| 66 | + } |
| 67 | + } |
| 68 | + if c.Gzip.MinLength == nil { |
| 69 | + c.Gzip.MinLength = &defaultGzipMinLength |
| 70 | + } |
| 71 | + |
| 72 | + if c.Gzip.CompressionLevel == nil { |
| 73 | + c.Gzip.CompressionLevel = &defaultGzipCompressionLevel |
| 74 | + } |
| 75 | + |
| 76 | + if *c.Gzip.MinLength <= 0 { |
| 77 | + return fmt.Errorf("invalid value for server.encoding.gzip.min_length field, should be a positive number") |
| 78 | + } |
| 79 | + |
| 80 | + acceptedCompressionLevels := map[int]bool{ |
| 81 | + gzip.NoCompression: true, |
| 82 | + gzip.BestSpeed: true, |
| 83 | + gzip.BestCompression: true, |
| 84 | + } |
| 85 | + _, compressionLevelAccepted := acceptedCompressionLevels[*c.Gzip.CompressionLevel] |
| 86 | + if !compressionLevelAccepted { |
| 87 | + return fmt.Errorf("invalid value for server.encoding.gzip.compression_level field, accepted values are 0, 1 or 9") |
| 88 | + } |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
0 commit comments