Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 35cb5ee

Browse files
committed
fixes
1 parent 8d66896 commit 35cb5ee

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ var (
345345
rbac.ResourceNotificationPreference.Type: {policy.ActionCreate, policy.ActionUpdate, policy.ActionDelete},
346346
rbac.ResourceNotificationTemplate.Type: {policy.ActionCreate, policy.ActionUpdate, policy.ActionDelete},
347347
rbac.ResourceCryptoKey.Type: {policy.ActionCreate, policy.ActionUpdate, policy.ActionDelete},
348-
rbac.ResourceFile.Type: {policy.ActionCreate},
348+
rbac.ResourceFile.Type: {policy.ActionCreate, policy.ActionRead},
349349
}),
350350
Org: map[string][]rbac.Permission{},
351351
User: []rbac.Permission{},

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,43 +1437,63 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob)
14371437
// If there is a plan, or a module files archive we need to insert a
14381438
// template_version_terraform_values row.
14391439
if len(plan) > 0 || len(moduleFiles) > 0 {
1440-
// ...but the plan and the module files archive are both optional! So
1441-
// we need to fallback to a valid JSON object if the plan was omitted.
1442-
if len(plan) == 0 {
1443-
plan = []byte("{}")
1444-
}
1445-
1446-
// ...and we only want to insert a files row if an archive was provided.
1447-
var fileID uuid.NullUUID
1448-
if len(moduleFiles) > 0 {
1449-
hashBytes := sha256.Sum256(moduleFiles)
1450-
hash := hex.EncodeToString(hashBytes[:])
1451-
// nolint:gocritic // Requires system privileges
1452-
file, err := s.Database.InsertFile(dbauthz.AsSystemRestricted(ctx), database.InsertFileParams{
1453-
ID: uuid.New(),
1454-
Hash: hash,
1455-
CreatedBy: uuid.Nil, // TODO
1456-
CreatedAt: dbtime.Now(),
1457-
Mimetype: tarMimeType,
1458-
Data: moduleFiles,
1440+
err := s.Database.InTx(func(tx database.Store) error {
1441+
// ...but the plan and the module files archive are both optional! So
1442+
// we need to fallback to a valid JSON object if the plan was omitted.
1443+
if len(plan) == 0 {
1444+
plan = []byte("{}")
1445+
}
1446+
1447+
// ...and we only want to insert a files row if an archive was provided.
1448+
var fileID uuid.NullUUID
1449+
if len(moduleFiles) > 0 {
1450+
hashBytes := sha256.Sum256(moduleFiles)
1451+
hash := hex.EncodeToString(hashBytes[:])
1452+
1453+
// nolint:gocritic // Requires reading "system" files
1454+
file, err := s.Database.GetFileByHashAndCreator(dbauthz.AsSystemRestricted(ctx), database.GetFileByHashAndCreatorParams{Hash: hash, CreatedBy: uuid.Nil})
1455+
if err == nil {
1456+
// This set of modules is already cached, which means we can reuse them
1457+
fileID = uuid.NullUUID{
1458+
Valid: true,
1459+
UUID: file.ID,
1460+
}
1461+
} else if !xerrors.Is(err, sql.ErrNoRows) {
1462+
return xerrors.Errorf("check for cached modules: %w", err)
1463+
} else {
1464+
// nolint:gocritic // Requires creating a "system" file
1465+
file, err = s.Database.InsertFile(dbauthz.AsSystemRestricted(ctx), database.InsertFileParams{
1466+
ID: uuid.New(),
1467+
Hash: hash,
1468+
CreatedBy: uuid.Nil,
1469+
CreatedAt: dbtime.Now(),
1470+
Mimetype: tarMimeType,
1471+
Data: moduleFiles,
1472+
})
1473+
if err != nil {
1474+
return xerrors.Errorf("insert template version terraform modules: %w", err)
1475+
}
1476+
fileID = uuid.NullUUID{
1477+
Valid: true,
1478+
UUID: file.ID,
1479+
}
1480+
}
1481+
}
1482+
1483+
err = s.Database.InsertTemplateVersionTerraformValuesByJobID(ctx, database.InsertTemplateVersionTerraformValuesByJobIDParams{
1484+
JobID: jobID,
1485+
UpdatedAt: now,
1486+
CachedPlan: plan,
1487+
CachedModuleFiles: fileID,
14591488
})
14601489
if err != nil {
1461-
return nil, xerrors.Errorf("insert template version terraform modules: %w", err)
1462-
}
1463-
fileID = uuid.NullUUID{
1464-
Valid: true,
1465-
UUID: file.ID,
1490+
return xerrors.Errorf("insert template version terraform data: %w", err)
14661491
}
1467-
}
14681492

1469-
err = s.Database.InsertTemplateVersionTerraformValuesByJobID(ctx, database.InsertTemplateVersionTerraformValuesByJobIDParams{
1470-
JobID: jobID,
1471-
UpdatedAt: now,
1472-
CachedPlan: plan,
1473-
CachedModuleFiles: fileID,
1474-
})
1493+
return nil
1494+
}, nil)
14751495
if err != nil {
1476-
return nil, xerrors.Errorf("insert template version terraform data: %w", err)
1496+
return nil, err
14771497
}
14781498
}
14791499

provisionerd/proto/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import "github.com/coder/coder/v2/apiversion"
1414
// - Add new field named `devcontainers` in the Agent.
1515
const (
1616
CurrentMajor = 1
17-
CurrentMinor = 4
17+
CurrentMinor = 5
1818
)
1919

2020
// CurrentVersion is the current provisionerd API version.

0 commit comments

Comments
 (0)