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

fix: validate that configs/secrets used match those provided #1423

Closed
wants to merge 8 commits into from

Conversation

deniseli
Copy link
Contributor

@deniseli deniseli commented May 7, 2024

Partially fixes #1173

The 3 commands dev, build, and deploy (which call instantiate a new buildengine) should all log warnings (for unnecessary configs/secrets) and errors (for configs/secrets required but not provided).

$ ftl build buildengine/testdata/projects/configsecret --config=buildengine/testdata/projectconfigs/config-secret-validation-ftl-project.toml
info:module: Building module
warn:builtin: config "someServiceAccessToken" is provided for module "module" in ftl-project.toml, but is not required
warn:builtin: secret "companyApiKey" is provided for module "module" in ftl-project.toml, but is not required
warn:builtin: config "ftlEndpointAlternate" is provided globally in ftl-project.toml, but is not required by any modules
ftl: error: config "missingConfig" is not provided in ftl-project.toml, but is required by module "module"
            secret "missingSecret" is not provided in ftl-project.toml, but is required by module "module"

Future PR to close the issue: ftl config/secret unset should fail if the value being removed is still referenced in a module, unless --force or -f is present, in which case we should warn instead of erroring.

@deniseli deniseli requested a review from a team as a code owner May 7, 2024 03:07
@deniseli deniseli requested review from matt2e and removed request for a team May 7, 2024 03:07
@alecthomas alecthomas mentioned this pull request May 7, 2024
@@ -82,10 +83,11 @@ func WithListener(listener Listener) Option {
// pull in missing schemas.
//
// "dirs" are directories to scan for local modules.
func New(ctx context.Context, client ftlv1connect.ControllerServiceClient, moduleDirs []string, externalDirs []string, options ...Option) (*Engine, error) {
func New(ctx context.Context, client ftlv1connect.ControllerServiceClient, projConfig *projectconfig.Config, moduleDirs []string, externalDirs []string, options ...Option) (*Engine, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

projectconfig.Config is not the appropriate type for this use. It is the raw configuration struct used to unmarshal TOML files, so it shouldn't be used outside that scope. ModuleContext OTOH is the appropriate type to use here, but IIRC there's no mechanism to get one prior to build. This needs some more thought, but my gut instinct is telling me we need an abstraction to retrieve ModuleConfigs from a projectconfig.Config.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if implementing the above is too much work we should abandon this ticket for now as it's not super high priority. Perhaps timebox it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhhhh got it, that explains a lot. I'll table this for now and pick up one of the next issues. We've talked a few times lately about possibly refactoring the interfaces for the TOML files, so it probably makes the most sense to come up with a holistic design for that that includes this use case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just happened to see #919 in next :) Just to make sure I'm reading correctly, is that issue basically the blocker to this one?

errs := []error{}
logger := log.FromContext(ctx)

configsProvidedGlobally := make(map[string]bool)
Copy link
Collaborator

@alecthomas alecthomas May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's quite a lot of duplication here. I think this could be refactored to use a function that takes a map of merged global+module entries and compares them to a map of used module entries, and returns errors with the differences. Pass in the "type" of entries (config or secret) as a string for more useful errors.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keys in the merged map could be in the form [<module>.]<key>

}
for secretName := range secretsUsed {
if _, isProvided := secretsProvided[secretName]; !isProvided {
errs = append(errs, fmt.Errorf("secret %q is not provided in ftl-project.toml, but is required by module %q", secretName, moduleName))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These errors should include the positional information from the schema.

// Index all provided configs into configsProvided and warn for unused configs
configsProvided := maps.Clone(globalConfig)
secretsProvided := maps.Clone(globalSecrets)
if e.projectConfig != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use pointers to convey optionality.

@deniseli deniseli closed this May 8, 2024
@deniseli
Copy link
Contributor Author

deniseli commented May 8, 2024

Intentionally leaving this branch alive for context, but closing the PR per #1423 (comment)

@matt2e matt2e added the approved Marks an already closed PR as approved label May 10, 2024
@matt2e matt2e removed their request for review May 10, 2024 05:54
@matt2e matt2e removed the approved Marks an already closed PR as approved label May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validate that the secrets and config required by the schema exactly match defined secrets
3 participants