Skip to content

Commit

Permalink
chore: disable sql connection when graphql is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhailswift authored and jkjell committed Oct 25, 2024
1 parent 08fae4a commit 35c1628
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 57 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,16 @@ file.

Archivista is configured through environment variables currently.

**Note**: If `ARCHIVISTA_ENABLE_SQL_STORE` is set to false no metadata about store attestations will be collected. Archivista will only store and retrieve attestations by Gitoid from it's storage.

| Variable | Default Value | Description |
| ------------------------------------------ | ----------------------------------------- | --------------------------------------------------------------------------------------------- |
| ARCHIVISTA_LISTEN_ON | tcp://127.0.0.1:8082 | URL endpoint for Archivista to listen on |
| ARCHIVISTA_READ_TIMEOUT | 120 | HTTP server read timeout |
| ARCHIVISTA_WRITE_TIMEOUT | 120 | HTTP server write timeout |
| ARCHIVISTA_LOG_LEVEL | INFO | Log level. Options are DEBUG, INFO, WARN, ERROR |
| ARCHIVISTA_CORS_ALLOW_ORIGINS | | Comma separated list of origins to allow CORS requests from |
| ARCHIVISTA_ENABLE_SQL_STORE | TRUE | Enable SQL Metadata store. If disabled, GraphQL will also be disabled |
| ARCHIVISTA_SQL_STORE_BACKEND | | Backend to use for SQL. Options are MYSQL or PSQL |
| ARCHIVISTA_SQL_STORE_CONNECTION_STRING | postgresql://root:example@tcp(db)/testify | SQL store connection string |
| ARCHIVISTA_STORAGE_BACKEND | | Backend to use for attestation storage. Options are FILE, BLOB, or empty string for disabled. |
Expand All @@ -118,7 +121,6 @@ Archivista is configured through environment variables currently.
| ARCHIVISTA_PUBLISHER_DAPR_URL | | Dapr full URL |
| ARCHIVISTA_PUBLISHER_RSTUF_HOST | | RSTUF URL |


## Using Archivista

Archivista exposes two HTTP endpoints to upload or download attestations:
Expand Down
52 changes: 29 additions & 23 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,40 @@
# See the License for the specific language governing permissions and
# limitations under the License.

if [[ -z $ARCHIVISTA_SQL_STORE_BACKEND ]]; then
SQL_TYPE="MYSQL"
ARCHIVISTA_ENABLE_SQL_STORE=$(echo ${ARCHIVISTA_ENABLE_SQL_STORE} | tr '[:lower:]' '[:upper:]')

if [ "${ARCHIVISTA_ENABLE_SQL_STORE}" = "FALSE" ]; then
echo "Skipping migrations"
else
if [[ -z $ARCHIVISTA_SQL_STORE_BACKEND ]]; then
SQL_TYPE="MYSQL"
else
SQL_TYPE=$(echo "$ARCHIVISTA_SQL_STORE_BACKEND" | tr '[:lower:]' '[:upper:]')
fi
case $SQL_TYPE in
MYSQL)
if [[ -z $ARCHIVISTA_SQL_STORE_CONNECTION_STRING ]]; then
ARCHIVISTA_SQL_STORE_CONNECTION_STRING="root:example@db/testify"
fi
echo "Running migrations for MySQL"
atlas migrate apply --dir "file:///archivista/migrations/mysql" --url "mysql://$ARCHIVISTA_SQL_STORE_CONNECTION_STRING"
atlas_rc=$?
;;
PSQL)
echo "Running migrations for Postgres"
atlas migrate apply --dir "file:///archivista/migrations/pgsql" --url "$ARCHIVISTA_SQL_STORE_CONNECTION_STRING"
atlas_rc=$?
;;
*)
echo "Unknown SQL backend: $ARCHIVISTA_SQL_STORE_BACKEND"
exit 1
;;
esac
fi
case $SQL_TYPE in
MYSQL)
if [[ -z $ARCHIVISTA_SQL_STORE_CONNECTION_STRING ]]; then
ARCHIVISTA_SQL_STORE_CONNECTION_STRING="root:example@db/testify"
fi
echo "Running migrations for MySQL"
atlas migrate apply --dir "file:///archivista/migrations/mysql" --url "mysql://$ARCHIVISTA_SQL_STORE_CONNECTION_STRING"
atlas_rc=$?
;;
PSQL)
echo "Running migrations for Postgres"
atlas migrate apply --dir "file:///archivista/migrations/pgsql" --url "$ARCHIVISTA_SQL_STORE_CONNECTION_STRING"
atlas_rc=$?
;;
*)
echo "Unknown SQL backend: $ARCHIVISTA_SQL_STORE_BACKEND"
exit 1
;;
esac

if [[ $atlas_rc -ne 0 ]]; then
if [[ $atlas_rc -ne 0 ]]; then
echo "Failed to apply migrations"
exit 1
fi
fi

/bin/archivista
3 changes: 2 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Config struct {
SPIFFEAddress string `default:"unix:///tmp/spire-agent/public/api.sock" desc:"SPIFFE server address" split_words:"true"`
SPIFFETrustedServerId string `default:"" desc:"Trusted SPIFFE server ID; defaults to any" split_words:"true"`

EnableSQLStore bool `default:"TRUE" desc:"*** Enable SQL Metadata store. If disabled, GraphQL will also be disabled ***" split_words:"true"`
SQLStoreConnectionString string `default:"root:example@tcp(db)/testify" desc:"SQL store connection string" split_words:"true"`
SQLStoreBackend string `default:"MYSQL" desc:"SQL backend to use. Options are MYSQL, PSQL" split_words:"true"`
SQLStoreMaxIdleConnections int `default:"10" desc:"Maximum number of connections in the idle connection pool" split_words:"true"`
Expand Down Expand Up @@ -86,7 +87,7 @@ func (c *Config) Process() error {
}
}

//check if both are being used and error if so
// check if both are being used and error if so
if usingDeprecatedEnv && usingNewEnv {
err := errors.New("both deprecated and new environment variables are being used. Please use only the new environment variables")
return err
Expand Down
13 changes: 7 additions & 6 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ func New(cfg *config.Config, opts ...Option) (Server, error) {
// TODO: remove from future version (v0.6.0) endpoint with version
r.HandleFunc("/download/{gitoid}", s.DownloadHandler)
r.HandleFunc("/upload", s.UploadHandler)
if cfg.EnableGraphql {
if cfg.EnableSQLStore && cfg.EnableGraphql && s.sqlClient != nil {
r.Handle("/query", s.Query(s.sqlClient))
r.Handle("/v1/query", s.Query(s.sqlClient))
}

r.HandleFunc("/v1/download/{gitoid}", s.DownloadHandler)
r.HandleFunc("/v1/upload", s.UploadHandler)
if cfg.GraphqlWebClientEnable {
if cfg.EnableSQLStore && cfg.EnableGraphql && cfg.GraphqlWebClientEnable {
r.Handle("/",
playground.Handler("Archivista", "/v1/query"),
)
Expand Down Expand Up @@ -171,9 +171,11 @@ func (s *Server) Upload(ctx context.Context, r io.Reader) (api.UploadResponse, e
}
}

if err := s.metadataStore.Store(ctx, gid.String(), payload); err != nil {
logrus.Errorf("received error from metadata store: %+v", err)
return api.UploadResponse{}, err
if s.metadataStore != nil {
if err := s.metadataStore.Store(ctx, gid.String(), payload); err != nil {
logrus.Errorf("received error from metadata store: %+v", err)
return api.UploadResponse{}, err
}
}

if s.publisherStore != nil {
Expand Down Expand Up @@ -321,7 +323,6 @@ func (s *Server) AllArtifactsHandler(w http.ResponseWriter, r *http.Request) {
}

w.Header().Set("Content-Type", "application/json")

}

// @Summary List Artifact Versions
Expand Down
53 changes: 27 additions & 26 deletions pkg/server/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,36 @@ func (a *ArchivistaService) Setup() (*Server, error) {
}
serverOpts = append(serverOpts, WithObjectStore(fileStore))

entClient, err := sqlstore.NewEntClient(
a.Cfg.SQLStoreBackend,
a.Cfg.SQLStoreConnectionString,
sqlstore.ClientWithMaxIdleConns(a.Cfg.SQLStoreMaxIdleConnections),
sqlstore.ClientWithMaxOpenConns(a.Cfg.SQLStoreMaxOpenConnections),
sqlstore.ClientWithConnMaxLifetime(a.Cfg.SQLStoreConnectionMaxLifetime),
)
if a.Cfg.EnableSQLStore {
entClient, err := sqlstore.NewEntClient(
a.Cfg.SQLStoreBackend,
a.Cfg.SQLStoreConnectionString,
sqlstore.ClientWithMaxIdleConns(a.Cfg.SQLStoreMaxIdleConnections),
sqlstore.ClientWithMaxOpenConns(a.Cfg.SQLStoreMaxOpenConnections),
sqlstore.ClientWithConnMaxLifetime(a.Cfg.SQLStoreConnectionMaxLifetime),
)
if err != nil {
logrus.Fatalf("could not create ent client: %+v", err)
}

if err != nil {
logrus.Fatalf("could not create ent client: %+v", err)
}
// Continue with the existing setup code for the SQLStore
sqlStore, a.sqlStoreCh, err = sqlstore.New(context.Background(), entClient)
if err != nil {
logrus.Fatalf("error initializing new SQLStore: %+v", err)
}
serverOpts = append(serverOpts, WithMetadataStore(sqlStore))

// Continue with the existing setup code for the SQLStore
sqlStore, a.sqlStoreCh, err = sqlstore.New(context.Background(), entClient)
if err != nil {
logrus.Fatalf("error initializing new SQLStore: %+v", err)
// Add SQL client for ent
sqlClient := sqlStore.GetClient()
serverOpts = append(serverOpts, WithEntSqlClient(sqlClient))
} else {
sqlStoreChan := make(chan error)
a.sqlStoreCh = sqlStoreChan
go func() {
<-a.Ctx.Done()
close(sqlStoreChan)
}()
}
serverOpts = append(serverOpts, WithMetadataStore(sqlStore))

// Add SQL client for ent
sqlClient := sqlStore.GetClient()
serverOpts = append(serverOpts, WithEntSqlClient(sqlClient))

// initialize the artifact store
if a.Cfg.EnableArtifactStore {
Expand All @@ -140,14 +148,7 @@ func (a *ArchivistaService) Setup() (*Server, error) {
logrus.Fatalf("could not create archivista server: %+v", err)
}

// Ensure background processes are managed
go func() {
<-a.sqlStoreCh
<-a.fileStoreCh
}()

logrus.WithField("duration", time.Since(now)).Infof("completed phase: initializing storage clients")

return &server, nil
}

Expand Down

0 comments on commit 35c1628

Please sign in to comment.