Visitar URL original
Add `runtime` field to policy packs (#23319) · pulumi/pulumi@e6a7bcc · GitHub
Skip to content

Commit e6a7bcc

Browse files
authored
Add runtime field to policy packs (#23319)
## Summary <!-- What changed and why. Link to issue if applicable. --> <!-- Remember: we squash-merge, so this description becomes the commit message. --> Added a `runtime` field to the SDK that is automatically populated on policy pack creation, ensuring that future policy pack updates only accept the same runtime name to prevent invisible changes. ## Test plan <!-- How did you test this change? --> - [x] Added appropriate unit tests - For all changes - [ ] Added a test in `pkg/engine/lifecycletest` - For all engine/protocol changes - [ ] Added a conformance test in `pkg/testing/pulumi-test-language` - For language protocol changes - [ ] Added a golden test in `pkg/backend/display` - For changes to the output renderers ## Validation <!-- Commands you ran. Do not include output, but make sure that you've run these and checked the results. --> - [x] `make lint` — clean - [x] `make test_fast` — all pass - [x] `make tidy_fix` — clean - [x] `make format` — clean - [x] Relevant SDK tests pass (if SDK changes) - [x] `make check_proto` — clean (if proto changes)
1 parent ceeab7d commit e6a7bcc

4 files changed

Lines changed: 64 additions & 8 deletions

File tree

‎pkg/backend/httpstate/client/client.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,7 @@ func (pc *Client) AssignTeamRole(
18831883
// PublishPolicyPack publishes a `PolicyPack` to the Pulumi service. If it successfully publishes
18841884
// the Policy Pack, it returns the version of the pack.
18851885
func (pc *Client) PublishPolicyPack(ctx context.Context, orgName string,
1886-
analyzerInfo plugin.AnalyzerInfo, dirArchive io.Reader,
1886+
runtime string, analyzerInfo plugin.AnalyzerInfo, dirArchive io.Reader,
18871887
metadata map[string]string,
18881888
) (string, error) {
18891889
//
@@ -1927,6 +1927,7 @@ func (pc *Client) PublishPolicyPack(ctx context.Context, orgName string,
19271927
Provider: analyzerInfo.Provider,
19281928
Tags: analyzerInfo.Tags,
19291929
Repository: analyzerInfo.Repository,
1930+
Runtime: runtime,
19301931
Metadata: metadata,
19311932
}
19321933

‎pkg/backend/httpstate/client/client_publish_policy_pack_test.go‎

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestPublishPolicyPack_AllAnalyzerInfoFieldsAreSent(t *testing.T) {
124124
var metadata map[string]string
125125

126126
// Call PublishPolicyPack
127-
version, err := client.PublishPolicyPack(t.Context(), "test-org", analyzerInfo, archive, metadata)
127+
version, err := client.PublishPolicyPack(t.Context(), "test-org", "python", analyzerInfo, archive, metadata)
128128
require.NoError(t, err)
129129
assert.Equal(t, "1.2.3", version)
130130

@@ -140,6 +140,7 @@ func TestPublishPolicyPack_AllAnalyzerInfoFieldsAreSent(t *testing.T) {
140140
assert.Equal(t, analyzerInfo.Provider, capturedRequest.Provider)
141141
assert.Equal(t, analyzerInfo.Tags, capturedRequest.Tags)
142142
assert.Equal(t, analyzerInfo.Repository, capturedRequest.Repository)
143+
assert.Equal(t, "python", capturedRequest.Runtime)
143144

144145
// Verify policies were converted correctly
145146
require.Len(t, capturedRequest.Policies, 1)
@@ -223,7 +224,7 @@ func TestPublishPolicyPack_EmptyOptionalFields(t *testing.T) {
223224
// Empty metadata.
224225
var metadata map[string]string
225226

226-
_, err := client.PublishPolicyPack(t.Context(), "test-org", analyzerInfo, archive, metadata)
227+
_, err := client.PublishPolicyPack(t.Context(), "test-org", "", analyzerInfo, archive, metadata)
227228
require.NoError(t, err)
228229

229230
// Verify required fields are present
@@ -238,6 +239,56 @@ func TestPublishPolicyPack_EmptyOptionalFields(t *testing.T) {
238239
assert.Empty(t, capturedRequest.Provider)
239240
assert.Empty(t, capturedRequest.Tags)
240241
assert.Empty(t, capturedRequest.Repository)
242+
assert.Empty(t, capturedRequest.Runtime)
243+
}
244+
245+
// TestPublishPolicyPack_RuntimeIsForwarded covers the runtime-disambiguation
246+
// guard added so the service can reject cross-runtime version bumps. The CLI
247+
// passes the policy pack project's runtime to PublishPolicyPack, and this test
248+
// verifies it lands in the request body.
249+
func TestPublishPolicyPack_RuntimeIsForwarded(t *testing.T) {
250+
t.Parallel()
251+
252+
analyzerInfo := plugin.AnalyzerInfo{
253+
Name: "runtime-test-pack",
254+
Version: "1.0.0",
255+
Policies: []plugin.AnalyzerPolicyInfo{
256+
{Name: "p1", EnforcementLevel: apitype.Advisory},
257+
},
258+
}
259+
260+
var capturedRuntime string
261+
var rawRequestBody []byte
262+
263+
server := newMockServerRequestProcessor(200, func(r *http.Request) string {
264+
if strings.HasSuffix(r.URL.Path, "/policypacks") && r.Method == "POST" {
265+
body, err := io.ReadAll(r.Body)
266+
require.NoError(t, err)
267+
rawRequestBody = body
268+
269+
var captured apitype.CreatePolicyPackRequest
270+
require.NoError(t, json.Unmarshal(body, &captured))
271+
capturedRuntime = captured.Runtime
272+
273+
resp := apitype.CreatePolicyPackResponse{
274+
Version: 1,
275+
UploadURI: "http://" + r.Host + "/upload",
276+
}
277+
respJSON, err := json.Marshal(resp)
278+
require.NoError(t, err)
279+
return string(respJSON)
280+
}
281+
return ""
282+
})
283+
defer server.Close()
284+
285+
client := newMockClient(server)
286+
_, err := client.PublishPolicyPack(t.Context(), "test-org", "opa",
287+
analyzerInfo, bytes.NewReader([]byte("data")), nil)
288+
require.NoError(t, err)
289+
290+
assert.Equal(t, "opa", capturedRuntime)
291+
assert.Contains(t, string(rawRequestBody), `"runtime":"opa"`)
241292
}
242293

243294
func TestPublishPolicyPack_LegacyVersionHandling(t *testing.T) {
@@ -293,7 +344,7 @@ func TestPublishPolicyPack_LegacyVersionHandling(t *testing.T) {
293344
// Empty metadata.
294345
var metadata map[string]string
295346

296-
version, err := client.PublishPolicyPack(t.Context(), "test-org", analyzerInfo, archive, metadata)
347+
version, err := client.PublishPolicyPack(t.Context(), "test-org", "", analyzerInfo, archive, metadata)
297348
require.NoError(t, err)
298349

299350
// Verify that server-assigned version is returned when client version is empty
@@ -390,7 +441,7 @@ func TestPublishPolicyPack_PolicyConfigSchemaConversion(t *testing.T) {
390441
// Empty metadata.
391442
var metadata map[string]string
392443

393-
_, err := client.PublishPolicyPack(t.Context(), "test-org", analyzerInfo, archive, metadata)
444+
_, err := client.PublishPolicyPack(t.Context(), "test-org", "", analyzerInfo, archive, metadata)
394445
require.NoError(t, err)
395446

396447
// Verify config schema conversion
@@ -472,7 +523,7 @@ func TestPublishPolicyPack_NilConfigSchema(t *testing.T) {
472523
// Empty metadata.
473524
var metadata map[string]string
474525

475-
_, err := client.PublishPolicyPack(t.Context(), "test-org", analyzerInfo, archive, metadata)
526+
_, err := client.PublishPolicyPack(t.Context(), "test-org", "", analyzerInfo, archive, metadata)
476527
require.NoError(t, err)
477528

478529
// Verify nil config schema is handled correctly
@@ -536,7 +587,7 @@ func TestPublishPolicyPack_NilComplianceFramework(t *testing.T) {
536587
// Empty metadata.
537588
var metadata map[string]string
538589

539-
_, err := client.PublishPolicyPack(t.Context(), "test-org", analyzerInfo, archive, metadata)
590+
_, err := client.PublishPolicyPack(t.Context(), "test-org", "", analyzerInfo, archive, metadata)
540591
require.NoError(t, err)
541592

542593
// Verify nil compliance framework is handled correctly

‎pkg/backend/httpstate/policypack.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func (pack *cloudPolicyPack) Publish(
450450
fmt.Println("Uploading policy pack to Pulumi service")
451451

452452
publishedVersion, err := pack.cl.PublishPolicyPack(
453-
ctx, pack.ref.orgName, analyzerInfo, bytes.NewReader(packTarball), op.Metadata)
453+
ctx, pack.ref.orgName, runtime, analyzerInfo, bytes.NewReader(packTarball), op.Metadata)
454454
if err != nil {
455455
return err
456456
}

‎sdk/go/common/apitype/policy.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ type CreatePolicyPackRequest struct {
5555
// A URL to the repository where the policy pack is defined.
5656
Repository string `json:"repository,omitempty"`
5757

58+
// Runtime identifies the analyzer SDK that produced the pack (e.g. "nodejs",
59+
// "python", "opa"). Empty when produced by an older SDK that does not report it.
60+
Runtime string `json:"runtime,omitempty"`
61+
5862
// Metadata contains optional data about the environment performing the publish operation,
5963
// e.g. the current source code control commit information.
6064
Metadata map[string]string `json:"metadata,omitempty"`

0 commit comments

Comments
 (0)