| 1 |
// Copyright 2022 The Archivista Contributors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package config |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"os" |
| 20 |
"strings" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/kelseyhightower/envconfig" |
| 24 |
"github.com/sirupsen/logrus" |
| 25 |
) |
| 26 |
|
| 27 |
type Config struct { |
| 28 |
ListenOn string `default:"tcp://127.0.0.1:8082" desc:"URL endpoint for Archivista to listen on" split_words:"true"` |
| 29 |
ReadTimeout int `default:"120" desc:"HTTP read timeout in seconds" split_words:"true"` |
| 30 |
WriteTimeout int `default:"120" desc:"HTTP write timeout in seconds" split_words:"true"` |
| 31 |
LogLevel string `default:"INFO" desc:"Log level" split_words:"true"` |
| 32 |
CORSAllowOrigins []string `default:"" desc:"Comma separated list of origins to allow CORS requests from" split_words:"true"` |
| 33 |
|
| 34 |
EnableTLS bool `default:"FALSE" desc:"Enables TLS on the Archivista server" split_words:"true"` |
| 35 |
TLSCert string `default:"" desc:"Path to the file containing the TLS Certificate" split_words:"true"` |
| 36 |
TLSKey string `default:"" desc:"Path to the file containing the TLS Key" split_words:"true"` |
| 37 |
|
| 38 |
EnableSPIFFE bool `default:"TRUE" desc:"*** Enable SPIFFE support" split_words:"true"` |
| 39 |
SPIFFEAddress string `default:"unix:///tmp/spire-agent/public/api.sock" desc:"SPIFFE server address" split_words:"true"` |
| 40 |
SPIFFETrustedServerId string `default:"" desc:"Trusted SPIFFE server ID; defaults to any" split_words:"true"` |
| 41 |
|
| 42 |
EnableSQLStore bool `default:"TRUE" desc:"*** Enable SQL Metadata store. If disabled, GraphQL will also be disabled ***" split_words:"true"` |
| 43 |
SQLStoreConnectionString string `default:"root:example@tcp(db)/testify" desc:"SQL store connection string" split_words:"true"` |
| 44 |
SQLStoreBackend string `default:"MYSQL" desc:"SQL backend to use. Options are MYSQL, PSQL" split_words:"true"` |
| 45 |
SQLStoreMaxIdleConnections int `default:"10" desc:"Maximum number of connections in the idle connection pool" split_words:"true"` |
| 46 |
SQLStoreMaxOpenConnections int `default:"100" desc:"Maximum number of open connections to the database" split_words:"true"` |
| 47 |
SQLStoreConnectionMaxLifetime time.Duration `default:"3m" desc:"Maximum amount of time a connection may be reused" split_words:"true"` |
| 48 |
|
| 49 |
StorageBackend string `default:"" desc:"Backend to use for attestation storage. Options are FILE, BLOB, or empty string for disabled." split_words:"true"` |
| 50 |
FileServeOn string `default:"" desc:"What address to serve files on. Only valid when using FILE storage backend." split_words:"true"` |
| 51 |
FileDir string `default:"/tmp/archivista/" desc:"Directory to store and serve files. Only valid when using FILE storage backend." split_words:"true"` |
| 52 |
BlobStoreEndpoint string `default:"127.0.0.1:9000" desc:"URL endpoint for blob storage. Only valid when using BLOB storage backend." split_words:"true"` |
| 53 |
BlobStoreCredentialType string `default:"ACCESS_KEY" desc:"Blob store credential type. Options are IAM or ACCESS_KEY" split_words:"true"` |
| 54 |
BlobStoreAccessKeyId string `default:"" desc:"Blob store access key id. Only valid when using BLOB storage backend." split_words:"true"` |
| 55 |
BlobStoreSecretAccessKeyId string `default:"" desc:"Blob store secret access key id. Only valid when using BLOB storage backend." split_words:"true"` |
| 56 |
BlobStoreUseTLS bool `default:"TRUE" desc:"Use TLS for BLOB storage backend. Only valid when using BLOB storage backend." split_words:"true"` |
| 57 |
BlobStoreBucketName string `default:"" desc:"Bucket to use for storage. Only valid when using BLOB storage backend." split_words:"true"` |
| 58 |
|
| 59 |
EnableGraphql bool `default:"TRUE" desc:"*** Enable GraphQL Endpoint. If GraphQL is disabled, Archivista will be unable to be used by Witness to verify policies" split_words:"true"` |
| 60 |
GraphqlWebClientEnable bool `default:"TRUE" desc:"Enable GraphiQL, the GraphQL web client" split_words:"true"` |
| 61 |
|
| 62 |
EnableArtifactStore bool `default:"FALSE" desc:"*** Enable Artifact Store Endpoints" split_words:"true"` |
| 63 |
ArtifactStoreConfig string `default:"/tmp/artifacts/config.yaml" desc:"Location of the config describing available artifacts" split_words:"true"` |
| 64 |
|
| 65 |
Publisher []string `default:"" desc:"Publisher to use. Options are DAPR, RSTUF or empty string for disabled." split_words:"true"` |
| 66 |
PublisherDaprHost string `default:"http://127.0.0.1" desc:"Host for Dapr" split_words:"true"` |
| 67 |
PublisherDaprPort string `default:"3500" desc:"Port for Dapr" split_words:"true"` |
| 68 |
PublisherDaprURL string `default:"" desc:"URL for Dapr" split_words:"true"` |
| 69 |
PublisherDaprComponentName string `default:"archivista" desc:"Dapr pubsub component name" split_words:"true"` |
| 70 |
PublisherDaprTopic string `default:"attestations" desc:"Dapr pubsub topic" split_words:"true"` |
| 71 |
PublisherRstufHost string `default:"http://127.0.0.1" desc:"Host for RSTUF" split_words:"true"` |
| 72 |
} |
| 73 |
|
| 74 |
// Process reads config from env |
| 75 |
func (c *Config) Process() error { |
| 76 |
if err := envconfig.Usage("archivista", c); err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
|
| 80 |
// Check if old env variables starting with ARCHIVIST_ are being used |
| 81 |
// TODO: Remove this in a future release |
| 82 |
usingDeprecatedEnv := false |
| 83 |
usingNewEnv := false |
| 84 |
for _, e := range os.Environ() { |
| 85 |
if strings.HasPrefix(e, "ARCHIVIST_") { |
| 86 |
usingDeprecatedEnv = true |
| 87 |
logrus.Warnf("Using deprecated environment variable %s. Please use ARCHIVISTA_ instead.", e) |
| 88 |
} |
| 89 |
if strings.HasPrefix(e, "ARCHIVISTA_") { |
| 90 |
usingNewEnv = true |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// check if both are being used and error if so |
| 95 |
if usingDeprecatedEnv && usingNewEnv { |
| 96 |
err := errors.New("both deprecated and new environment variables are being used. Please use only the new environment variables") |
| 97 |
return err |
| 98 |
} |
| 99 |
|
| 100 |
if usingDeprecatedEnv { |
| 101 |
err := envconfig.Process("archivist", c) |
| 102 |
if err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if usingNewEnv { |
| 108 |
err := envconfig.Process("archivista", c) |
| 109 |
if err != nil { |
| 110 |
return err |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return nil |
| 115 |
} |