Skip to content

Commit

Permalink
Display warning when reading a buf.lock with a b3 digest (#2520)
Browse files Browse the repository at this point in the history
Right now, if the CLI reads a buf.lock with a b3 digest, it treats
digest as empty. This PR displays a warning when it's encountered.

Added a check that reads the lock file and checks whether any digest
starts with `b1-` or `b3-` to
- `getSourceModuleConfig`, called by `buf beta graph`, `buf beta price`,
`buf beta stats`, `buf build`, `buf convert` , `buf export`, `buf
format`, `buf generate`and `buf lint`, when the source / input is a
module directory or workspace directory.
-  `run` in `buf push`.
  • Loading branch information
oliversun9 authored Nov 9, 2023
1 parent 6befbe4 commit b38f0f7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
12 changes: 8 additions & 4 deletions private/buf/bufwire/module_config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/bufbuild/buf/private/buf/buffetch"
"github.com/bufbuild/buf/private/buf/bufwork"
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
"github.com/bufbuild/buf/private/bufpkg/buflock"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmodulebuild"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
Expand Down Expand Up @@ -490,6 +491,13 @@ func (m *moduleConfigReader) getSourceModuleConfig(
externalExcludeDirOrFilePaths []string,
externalDirOrFilePathsAllowNotExist bool,
) (ModuleConfig, error) {
mappedReadBucket := readBucket
if subDirPath != "." {
mappedReadBucket = storage.MapReadBucket(readBucket, storage.MapOnPrefix(subDirPath))
}
if err := buflock.CheckDeprecatedDigests(ctx, m.logger, mappedReadBucket); err != nil {
return nil, err
}
if module, moduleConfig, ok := workspaceBuilder.GetModuleConfig(subDirPath); ok {
// The module was already built while we were constructing the workspace.
// However, we still need to perform some additional validation based on
Expand All @@ -507,10 +515,6 @@ func (m *moduleConfigReader) getSourceModuleConfig(
}
return newModuleConfig(module, moduleConfig), nil
}
mappedReadBucket := readBucket
if subDirPath != "." {
mappedReadBucket = storage.MapReadBucket(readBucket, storage.MapOnPrefix(subDirPath))
}
moduleConfig, err := bufconfig.ReadConfigOS(
ctx,
mappedReadBucket,
Expand Down
4 changes: 4 additions & 0 deletions private/buf/cmd/buf/command/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
"github.com/bufbuild/buf/private/bufpkg/bufcas"
"github.com/bufbuild/buf/private/bufpkg/bufcas/bufcasalpha"
"github.com/bufbuild/buf/private/bufpkg/buflock"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmodulebuild"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/gen/proto/connect/buf/alpha/registry/v1alpha1/registryv1alpha1connect"
Expand Down Expand Up @@ -202,6 +203,9 @@ func run(
if err != nil {
return err
}
if err := buflock.CheckDeprecatedDigests(ctx, container.Logger(), sourceBucket); err != nil {
return err
}
moduleIdentity := sourceConfig.ModuleIdentity
builtModule, err := bufmodulebuild.NewModuleBucketBuilder().BuildForBucket(
ctx,
Expand Down
11 changes: 11 additions & 0 deletions private/bufpkg/buflock/buflock.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/bufbuild/buf/private/pkg/storage"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -59,6 +60,16 @@ func WriteConfig(ctx context.Context, writeBucket storage.WriteBucket, config *C
return writeConfig(ctx, writeBucket, config)
}

// CheckDeprecatedDigests prints a warning if a lock file exists at ExternalConfigFilePath
// and has deprecated digest formats.
func CheckDeprecatedDigests(
ctx context.Context,
logger *zap.Logger,
readBucket storage.ReadBucket,
) error {
return checkDeprecatedDigests(ctx, logger, readBucket)
}

// ExternalConfigV1 represents the v1 lock file.
type ExternalConfigV1 struct {
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Expand Down
44 changes: 44 additions & 0 deletions private/bufpkg/buflock/lock_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ import (
"errors"
"fmt"
"io/fs"
"strings"

"github.com/bufbuild/buf/private/pkg/encoding"
"github.com/bufbuild/buf/private/pkg/storage"
"go.uber.org/zap"
)

var deprecatedFormatToPrefix = map[string]string{
"b1": "b1-",
"b3": "b3-",
}

func readConfig(ctx context.Context, readBucket storage.ReadBucket) (_ *Config, retErr error) {
configBytes, err := storage.ReadPath(ctx, readBucket, ExternalConfigFilePath)
if err != nil {
Expand Down Expand Up @@ -85,3 +92,40 @@ func writeConfig(ctx context.Context, writeBucket storage.WriteBucket, config *C
}
return nil
}

func checkDeprecatedDigests(
ctx context.Context,
logger *zap.Logger,
readBucket storage.ReadBucket,
) error {
configBytes, err := storage.ReadPath(ctx, readBucket, ExternalConfigFilePath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return fmt.Errorf("failed to read lock file: %w", err)
}
var configVersion ExternalConfigVersion
if err := encoding.UnmarshalYAMLNonStrict(configBytes, &configVersion); err != nil {
return fmt.Errorf("failed to decode lock file as YAML: %w", err)
}
if configVersion.Version != V1Version {
return nil
}
var externalConfig ExternalConfigV1
if err := encoding.UnmarshalYAMLStrict(configBytes, &externalConfig); err != nil {
return fmt.Errorf("failed to unmarshal lock file at %s: %w", V1Version, err)
}
for _, dep := range externalConfig.Deps {
for deprecatedFormat, prefix := range deprecatedFormatToPrefix {
if strings.HasPrefix(dep.Digest, prefix) {
logger.Sugar().Warnf(
`found %s digest in buf.yaml, which will no longer be supported in a future version. Run "buf mod update" to update the lock file.`,
deprecatedFormat,
)
return nil
}
}
}
return nil
}

0 comments on commit b38f0f7

Please sign in to comment.