Skip to content

Run buf Policies locally #3785

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
18 changes: 17 additions & 1 deletion private/buf/bufctl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type ImageWithConfig interface {
LintConfig() bufconfig.LintConfig
BreakingConfig() bufconfig.BreakingConfig
PluginConfigs() []bufconfig.PluginConfig
PolicyConfigs() []bufconfig.PolicyConfig

isImageWithConfig()
}
Expand Down Expand Up @@ -404,7 +405,10 @@ func (c *controller) GetTargetImageWithConfigsAndCheckClient(
}
lintConfig := bufconfig.DefaultLintConfigV1
breakingConfig := bufconfig.DefaultBreakingConfigV1
var pluginConfigs []bufconfig.PluginConfig
var (
pluginConfigs []bufconfig.PluginConfig
policyConfigs []bufconfig.PolicyConfig
)
pluginKeyProvider := bufplugin.NopPluginKeyProvider
bufYAMLFile, err := bufconfig.GetBufYAMLFileForPrefixOrOverride(
ctx,
Expand Down Expand Up @@ -442,6 +446,7 @@ func (c *controller) GetTargetImageWithConfigsAndCheckClient(
// buf.yaml file is found, the PluginConfigs from the buf.yaml file and the PluginKeys
// from the buf.lock file are resolved to create the PluginKeyProvider.
pluginConfigs = bufYAMLFile.PluginConfigs()
policyConfigs = bufYAMLFile.PolicyConfigs()
// If a config override is provided, the PluginConfig remote Refs use the BSR
// to resolve the PluginKeys. No buf.lock is required.
// If the buf.yaml file is not found, the bufplugin.NopPluginKeyProvider is returned.
Expand Down Expand Up @@ -485,6 +490,7 @@ func (c *controller) GetTargetImageWithConfigsAndCheckClient(
lintConfig,
breakingConfig,
pluginConfigs,
policyConfigs,
),
}
checkClient, err := bufcheck.NewClient(
Expand All @@ -495,6 +501,7 @@ func (c *controller) GetTargetImageWithConfigsAndCheckClient(
),
bufcheck.ClientWithLocalWasmPluginsFromOS(),
bufcheck.ClientWithRemoteWasmPlugins(pluginKeyProvider, c.pluginDataProvider),
bufcheck.ClientWithLocalPolicies(bucket),
)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -801,6 +808,13 @@ func (c *controller) GetCheckClientForWorkspace(
if err != nil {
return nil, err
}
bucket, err := c.storageosProvider.NewReadWriteBucket(
".",
storageos.ReadWriteBucketWithSymlinksIfSupported(),
)
if err != nil {
return nil, err
}
return bufcheck.NewClient(
c.logger,
bufcheck.ClientWithStderr(c.container.Stderr()),
Expand All @@ -812,6 +826,7 @@ func (c *controller) GetCheckClientForWorkspace(
pluginKeyProvider,
c.pluginDataProvider,
),
bufcheck.ClientWithLocalPolicies(bucket),
)
}

Expand Down Expand Up @@ -1182,6 +1197,7 @@ func (c *controller) buildTargetImageWithConfigs(
workspace.GetLintConfigForOpaqueID(module.OpaqueID()),
workspace.GetBreakingConfigForOpaqueID(module.OpaqueID()),
workspace.PluginConfigs(),
workspace.PolicyConfigs(),
),
)
}
Expand Down
7 changes: 7 additions & 0 deletions private/buf/bufctl/image_with_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type imageWithConfig struct {
lintConfig bufconfig.LintConfig
breakingConfig bufconfig.BreakingConfig
pluginConfigs []bufconfig.PluginConfig
policyConfigs []bufconfig.PolicyConfig
}

func newImageWithConfig(
Expand All @@ -37,6 +38,7 @@ func newImageWithConfig(
lintConfig bufconfig.LintConfig,
breakingConfig bufconfig.BreakingConfig,
pluginConfigs []bufconfig.PluginConfig,
policyConfigs []bufconfig.PolicyConfig,
) *imageWithConfig {
return &imageWithConfig{
Image: image,
Expand All @@ -45,6 +47,7 @@ func newImageWithConfig(
lintConfig: lintConfig,
breakingConfig: breakingConfig,
pluginConfigs: pluginConfigs,
policyConfigs: policyConfigs,
}
}

Expand All @@ -68,4 +71,8 @@ func (i *imageWithConfig) PluginConfigs() []bufconfig.PluginConfig {
return i.pluginConfigs
}

func (i *imageWithConfig) PolicyConfigs() []bufconfig.PolicyConfig {
return i.policyConfigs
}

func (*imageWithConfig) isImageWithConfig() {}
2 changes: 2 additions & 0 deletions private/buf/buflsp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ func (f *file) RunLints(ctx context.Context) bool {
f.workspace.GetLintConfigForOpaqueID(f.module.OpaqueID()),
f.image,
bufcheck.WithPluginConfigs(f.workspace.PluginConfigs()...),
bufcheck.WithPolicyConfigs(f.workspace.PolicyConfigs()...),
))
}

Expand Down Expand Up @@ -749,6 +750,7 @@ func (f *file) RunBreaking(ctx context.Context) bool {
f.image,
f.againstImage,
bufcheck.WithPluginConfigs(f.workspace.PluginConfigs()...),
bufcheck.WithPolicyConfigs(f.workspace.PolicyConfigs()...),
))
}

Expand Down
21 changes: 21 additions & 0 deletions private/buf/bufworkspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
"github.com/bufbuild/buf/private/bufpkg/bufpolicy"
)

// Workspace is a buf workspace.
Expand Down Expand Up @@ -81,6 +82,14 @@ type Workspace interface {
//
// These come from the buf.lock file. Only v2 supports plugins.
RemotePluginKeys() []bufplugin.PluginKey
// PolicyConfigs gets the configured PolicyConfigs of the Workspace.
//
// These come from the buf.yaml files.
PolicyConfigs() []bufconfig.PolicyConfig
// RemotePolicyKeys gets the remote PolicyKeys of the Workspace.
//
// These come from the buf.lock file. Only v2 supports policies.
RemotePolicyKeys() []bufpolicy.PolicyKey
// ConfiguredDepModuleRefs returns the configured dependencies of the Workspace as Refs.
//
// These come from buf.yaml files.
Expand Down Expand Up @@ -114,6 +123,8 @@ type workspace struct {
opaqueIDToBreakingConfig map[string]bufconfig.BreakingConfig
pluginConfigs []bufconfig.PluginConfig
remotePluginKeys []bufplugin.PluginKey
policyConfigs []bufconfig.PolicyConfig
remotePolicyKeys []bufpolicy.PolicyKey
configuredDepModuleRefs []bufparse.Ref

// If true, the workspace was created from v2 buf.yamls.
Expand All @@ -127,6 +138,7 @@ func newWorkspace(
opaqueIDToBreakingConfig map[string]bufconfig.BreakingConfig,
pluginConfigs []bufconfig.PluginConfig,
remotePluginKeys []bufplugin.PluginKey,
policyConfigs []bufconfig.PolicyConfig,
configuredDepModuleRefs []bufparse.Ref,
isV2 bool,
) *workspace {
Expand All @@ -136,6 +148,7 @@ func newWorkspace(
opaqueIDToBreakingConfig: opaqueIDToBreakingConfig,
pluginConfigs: pluginConfigs,
remotePluginKeys: remotePluginKeys,
policyConfigs: policyConfigs,
configuredDepModuleRefs: configuredDepModuleRefs,
isV2: isV2,
}
Expand All @@ -157,6 +170,14 @@ func (w *workspace) RemotePluginKeys() []bufplugin.PluginKey {
return slices.Clone(w.remotePluginKeys)
}

func (w *workspace) PolicyConfigs() []bufconfig.PolicyConfig {
return slices.Clone(w.policyConfigs)
}

func (w *workspace) RemotePolicyKeys() []bufpolicy.PolicyKey {
return slices.Clone(w.remotePolicyKeys)
}

func (w *workspace) ConfiguredDepModuleRefs() []bufparse.Ref {
return slices.Clone(w.configuredDepModuleRefs)
}
Expand Down
8 changes: 8 additions & 0 deletions private/buf/bufworkspace/workspace_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (w *workspaceProvider) GetWorkspaceForModuleKey(
var (
pluginConfigs []bufconfig.PluginConfig
remotePluginKeys []bufplugin.PluginKey
policyConfigs []bufconfig.PolicyConfig
)
if config.configOverride != "" {
bufYAMLFile, err := bufconfig.GetBufYAMLFileForOverride(config.configOverride)
Expand Down Expand Up @@ -205,6 +206,8 @@ func (w *workspaceProvider) GetWorkspaceForModuleKey(
return nil, err
}
}

policyConfigs = bufYAMLFile.PolicyConfigs()
}
}

Expand Down Expand Up @@ -243,6 +246,7 @@ func (w *workspaceProvider) GetWorkspaceForModuleKey(
opaqueIDToBreakingConfig,
pluginConfigs,
remotePluginKeys,
policyConfigs,
nil,
false,
), nil
Expand Down Expand Up @@ -408,6 +412,7 @@ func (w *workspaceProvider) getWorkspaceForBucketAndModuleDirPathsV1Beta1OrV1(
v1WorkspaceTargeting.bucketIDToModuleConfig,
nil, // No PluginConfigs for v1
nil, // No remote PluginKeys for v1
nil, // No PolicyConfigs for v1
v1WorkspaceTargeting.allConfiguredDepModuleRefs,
false,
)
Expand Down Expand Up @@ -507,6 +512,7 @@ func (w *workspaceProvider) getWorkspaceForBucketBufYAMLV2(
v2Targeting.bucketIDToModuleConfig,
v2Targeting.bufYAMLFile.PluginConfigs(),
remotePluginKeys,
v2Targeting.bufYAMLFile.PolicyConfigs(),
v2Targeting.bufYAMLFile.ConfiguredDepModuleRefs(),
true,
)
Expand All @@ -518,6 +524,7 @@ func (w *workspaceProvider) getWorkspaceForBucketModuleSet(
bucketIDToModuleConfig map[string]bufconfig.ModuleConfig,
pluginConfigs []bufconfig.PluginConfig,
remotePluginKeys []bufplugin.PluginKey,
policyConfigs []bufconfig.PolicyConfig,
// Expected to already be unique by FullName.
configuredDepModuleRefs []bufparse.Ref,
isV2 bool,
Expand All @@ -544,6 +551,7 @@ func (w *workspaceProvider) getWorkspaceForBucketModuleSet(
opaqueIDToBreakingConfig,
pluginConfigs,
remotePluginKeys,
policyConfigs,
configuredDepModuleRefs,
isV2,
), nil
Expand Down
1 change: 1 addition & 0 deletions private/buf/cmd/buf/command/breaking/breaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ func run(
for i, imageWithConfig := range imageWithConfigs {
breakingOptions := []bufcheck.BreakingOption{
bufcheck.WithPluginConfigs(imageWithConfig.PluginConfigs()...),
bufcheck.WithPolicyConfigs(imageWithConfig.PolicyConfigs()...),
bufcheck.WithRelatedCheckConfigs(allCheckConfigs...),
}
if flags.ExcludeImports {
Expand Down
1 change: 1 addition & 0 deletions private/buf/cmd/buf/command/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func run(
for _, imageWithConfig := range imageWithConfigs {
lintOptions := []bufcheck.LintOption{
bufcheck.WithPluginConfigs(imageWithConfig.PluginConfigs()...),
bufcheck.WithPolicyConfigs(imageWithConfig.PolicyConfigs()...),
bufcheck.WithRelatedCheckConfigs(allCheckConfigs...),
}
if err := checkClient.Lint(
Expand Down
7 changes: 7 additions & 0 deletions private/bufpkg/bufanalysis/bufanalysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ type FileAnnotation interface {
// May be empty if this annotation did not originate from a plugin.
// This may be added to the printed message field for certain printers.
PluginName() string
// PolicyName is the name of the policy that the annotation originated from.
//
// May be empty if this annotation did not originate from a policy.
// This may be added to the printed message field for certain printers.
PolicyName() string

isFileAnnotation()
}
Expand All @@ -163,6 +168,7 @@ func NewFileAnnotation(
typeString string,
message string,
pluginName string,
policyName string,
) FileAnnotation {
return newFileAnnotation(
fileInfo,
Expand All @@ -173,6 +179,7 @@ func NewFileAnnotation(
typeString,
message,
pluginName,
policyName,
)
}

Expand Down
Loading