Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add allowed origins config #1408

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,19 @@ Note: `sudo` is needed above for the redirection server (to bind port 80).

A valid email address is necessary for the creation of the certificate, and is important to get notifications from the Certificate Authority - in case the certificate is about to expire, etc.

## Supporting CORS

When accessing DefraDB through a frontend interface, you may be confronted with a CORS error. That is because, by default, DefraDB will not have any allowed origins set. To specify which origins should be allowed to access your DefraDB endpoint, you can specify them when starting the database:
```shell
defradb start --allowe-dorigins=https://yourdomain.com
```

If running a frontend app locally on localhost, allowed origins must be set with the port of the app:
```shell
defradb start --allowed-origins=http://localhost:3000
```

The catch-all `*` is also a valid origin.

## Community

Expand Down
10 changes: 10 additions & 0 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ func MakeStartCommand(cfg *config.Config) *cobra.Command {
log.FeedbackFatalE(context.Background(), "Could not bind api.tls", err)
}

cmd.Flags().StringArray(
"allowed-origins", cfg.API.AllowedOrigins,
"List of origins to allow for CORS requests",
)
err = cfg.BindFlag("api.allowed-origins", cmd.Flags().Lookup("allowed-origins"))
if err != nil {
log.FeedbackFatalE(context.Background(), "Could not bind api.allowed-origins", err)
}

cmd.Flags().String(
"pubkeypath", cfg.API.PubKeyPath,
"Path to the public key for tls",
Expand Down Expand Up @@ -319,6 +328,7 @@ func start(ctx context.Context, cfg *config.Config) (*defraInstance, error) {
sOpt := []func(*httpapi.Server){
httpapi.WithAddress(cfg.API.Address),
httpapi.WithRootDir(cfg.Rootdir),
httpapi.WithAllowedOrigins(cfg.API.AllowedOrigins...),
}

if n != nil {
Expand Down
22 changes: 12 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,22 @@ func (dbcfg DatastoreConfig) validate() error {

// APIConfig configures the API endpoints.
type APIConfig struct {
Address string
TLS bool
PubKeyPath string
PrivKeyPath string
Email string
Address string
TLS bool
AllowedOrigins []string `mapstructure:"allowed-origins"`
PubKeyPath string
PrivKeyPath string
Email string
}

func defaultAPIConfig() *APIConfig {
return &APIConfig{
Address: "localhost:9181",
TLS: false,
PubKeyPath: "certs/server.key",
PrivKeyPath: "certs/server.crt",
Email: DefaultAPIEmail,
Address: "localhost:9181",
TLS: false,
AllowedOrigins: []string{},
PubKeyPath: "certs/server.key",
PrivKeyPath: "certs/server.crt",
Email: DefaultAPIEmail,
}
}

Expand Down
2 changes: 2 additions & 0 deletions config/configfile_yaml.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ api:
address: {{ .API.Address }}
# Whether the API server should listen over HTTPS
tls: {{ .API.TLS }}
# The list of origins a cross-domain request can be executed from.
# allowed-origins: {{ .API.AllowedOrigins }}
# The path to the public key file. Ignored if domains is set.
pubkeypath: {{ .API.PubKeyPath }}
# The path to the private key file. Ignored if domains is set.
Expand Down