Skip to content

Commit

Permalink
Fix G301 gosec issues
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dimitrov <[email protected]>
  • Loading branch information
dimitarvdimitrov committed Jan 9, 2025
1 parent d68b6ad commit 3249ae0
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ linters-settings:
- G109
- G112
- G114
- G301
- G302

run:
Expand Down
6 changes: 3 additions & 3 deletions pkg/alertmanager/multitenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ type MultitenantAlertmanager struct {

// NewMultitenantAlertmanager creates a new MultitenantAlertmanager.
func NewMultitenantAlertmanager(cfg *MultitenantAlertmanagerConfig, store alertstore.AlertStore, limits Limits, features featurecontrol.Flagger, logger log.Logger, registerer prometheus.Registerer) (*MultitenantAlertmanager, error) {
err := os.MkdirAll(cfg.DataDir, 0777)
err := os.MkdirAll(cfg.DataDir, 0750)
if err != nil {
return nil, fmt.Errorf("unable to create Alertmanager data directory %q: %s", cfg.DataDir, err)
}
Expand Down Expand Up @@ -915,7 +915,7 @@ func (am *MultitenantAlertmanager) newAlertmanager(userID string, amConfig *defi
reg := prometheus.NewRegistry()

tenantDir := am.getTenantDirectory(userID)
err := os.MkdirAll(tenantDir, 0777)
err := os.MkdirAll(tenantDir, 0750)
if err != nil {
return nil, errors.Wrapf(err, "failed to create per-tenant directory %v", tenantDir)
}
Expand Down Expand Up @@ -1368,7 +1368,7 @@ func safeTemplateFilepath(dir, templateName string) (string, error) {
func storeTemplateFile(templateFilepath, content string) (bool, error) {
// Make sure the directory exists.
dir := filepath.Dir(templateFilepath)
err := os.MkdirAll(dir, 0755)
err := os.MkdirAll(dir, 0750)
if err != nil {
return false, fmt.Errorf("unable to create Alertmanager templates directory %q: %s", dir, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/blockbuilder/blockbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (b *BlockBuilder) starting(context.Context) (err error) {
if err := os.RemoveAll(b.cfg.DataDir); err != nil {
return fmt.Errorf("removing data dir: %w", err)
}
if err := os.MkdirAll(b.cfg.DataDir, os.ModePerm); err != nil {
if err := os.MkdirAll(b.cfg.DataDir, 0750); err != nil {
return fmt.Errorf("creating data dir: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/blockbuilder/tsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (b *TSDBBuilder) newTSDB(tenant tsdbTenant) (*userTSDB, error) {
if err := os.RemoveAll(udir); err != nil {
return nil, err
}
if err := os.MkdirAll(udir, os.ModePerm); err != nil {
if err := os.MkdirAll(udir, 0750); err != nil {
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/mimirtool/commands/remote_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func (c *RemoteReadCommand) export(_ *kingpin.ParseContext) error {
log.Infof("Created TSDB in path '%s'", c.tsdbPath)
} else {
if _, err := os.Stat(c.tsdbPath); err != nil && os.IsNotExist(err) {
if err = os.Mkdir(c.tsdbPath, 0755); err != nil {
if err = os.Mkdir(c.tsdbPath, 0750); err != nil {
return err
}
log.Infof("Created TSDB in path '%s'", c.tsdbPath)
Expand Down Expand Up @@ -483,7 +483,7 @@ func (c *RemoteReadCommand) export(_ *kingpin.ParseContext) error {
// ensure that tsdb directory has WAL, otherwise 'promtool tsdb dump' fails
walPath := filepath.Join(c.tsdbPath, "wal")
if _, err := os.Stat(walPath); err != nil && os.IsNotExist(err) {
if err := os.Mkdir(walPath, 0755); err != nil {
if err := os.Mkdir(walPath, 0750); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/tsdb/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Download(ctx context.Context, logger log.Logger, bucket objstore.Bucket, id
_, err := os.Stat(chunksDir)
if os.IsNotExist(err) {
// This can happen if block is empty. We cannot easily upload empty directory, so create one here.
return os.Mkdir(chunksDir, os.ModePerm)
return os.Mkdir(chunksDir, 0750)
}
if err != nil {
return errors.Wrapf(err, "stat %s", chunksDir)
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/tsdb/block/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func NewMetaFetcher(logger log.Logger, concurrency int, bkt objstore.Instrumente
cacheDir := ""
if dir != "" {
cacheDir = filepath.Join(dir, "meta-syncer")
if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {
if err := os.MkdirAll(cacheDir, 0750); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func (f *MetaFetcher) loadMeta(ctx context.Context, id ulid.ULID) (*Meta, error)

// Best effort cache in local dir.
if f.cacheDir != "" {
if err := os.MkdirAll(cachedBlockDir, os.ModePerm); err != nil {
if err := os.MkdirAll(cachedBlockDir, 0750); err != nil {
level.Warn(f.logger).Log("msg", "best effort mkdir of the meta.json block dir failed; ignoring", "dir", cachedBlockDir, "err", err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/storegateway/indexheader/binary_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func newBinaryWriter(fn string, buf []byte) (w *binaryWriter, err error) {

df, err := fileutil.OpenDir(dir)
if os.IsNotExist(err) {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
if err := os.MkdirAll(dir, 0750); err != nil {
return nil, err
}
df, err = fileutil.OpenDir(dir)
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/test/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func copyRecursive(src, dst string) error {
}

if info.IsDir() {
return os.MkdirAll(filepath.Join(dst, relPath), os.ModePerm)
return os.MkdirAll(filepath.Join(dst, relPath), 0700)
}

if !info.Mode().IsRegular() {
Expand Down
2 changes: 1 addition & 1 deletion tools/benchmark-query-engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (a *app) createTempDir() error {
slog.Info("created temporary directory", "dir", a.tempDir)

a.dataDir = filepath.Join(a.tempDir, "data")
if err := os.Mkdir(a.dataDir, 0777); err != nil {
if err := os.Mkdir(a.dataDir, 0700); err != nil {
return fmt.Errorf("could not create data directory '%v': %w", a.dataDir, err)
}

Expand Down

0 comments on commit 3249ae0

Please sign in to comment.