Skip to content

Commit 313bfcb

Browse files
committed
Harden custom stack validation and clarify credential scope
Reject empty image repository path components and align staging lifecycle asset cache keys with the resolved platform stack. Document build-only versus app-level registry credentials, add regression coverage, and remove redundant tests and upload action refactoring.
1 parent 202232d commit 313bfcb

9 files changed

Lines changed: 110 additions & 65 deletions

File tree

‎docs/v3/source/includes/concepts/_lifecycles.md.erb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Name | Type | Description
4141
**type** | _string_ | `buildpack`
4242
**data.buildpacks** | _list of strings_ | A list of the names of buildpacks, URLs from which they may be downloaded, or `null` to auto-detect a suitable buildpack during staging
4343
**data.stack** | _string_ | The root filesystem to use with the buildpack, for example `cflinuxfs4`. <br/>When the `diego_custom_stacks` feature flag is enabled, this can also be a container image URI with the `docker://` scheme. A custom stack requires custom buildpacks or admin buildpacks that are not associated with a stack. Its image layers count toward the app instance's disk quota.
44-
**data.credentials** | _object_ | Credentials used to download custom buildpacks from private Git repositories or to pull custom stacks from private registries, keyed by host. Each entry contains a username/password pair. If any buildpack URL embeds credentials, **data.credentials** must be empty; provide credentials in one place only.
44+
**data.credentials** | _object_ | Credentials used to download custom buildpacks from private Git repositories or to pull custom stacks from private registries, keyed by host. Each entry contains a username/password pair. If any buildpack URL embeds credentials, **data.credentials** must be empty; provide credentials in one place only. <br/>For private custom stacks, configure credentials on the app lifecycle for running instances and tasks.
4545

4646
During staging with a custom stack, Cloud Foundry uses the final repository component of the image URI, excluding its tag or digest, as `CF_STACK`. For example, `docker://registry.example.com/team/cflinuxfs4:1.268.0` sets `CF_STACK` to `cflinuxfs4`. The custom stack and buildpack must be compatible.
4747

@@ -86,7 +86,7 @@ Name | Type | Description
8686
---- | ---- | -----------
8787
**type** | _string_ | `cnb`
8888
**data.buildpacks** | _list of strings_ | A list of URLs with either `docker://` or `http(s)://` scheme, pointing to the Cloud Native Buildpack OCI image. <br/>When the scheme is `http(s)://`, an OCI tarball is expected to be present at the specified location.
89-
**data.credentials** | _object_ | Credentials used to download the configured buildpacks or to pull custom stacks, keyed by registry host. Each entry contains a username/password pair or a token. Custom stack credentials must be a username/password pair. CNB buildpack URLs must not embed credentials; use **data.credentials** instead.
89+
**data.credentials** | _object_ | Credentials used to download the configured buildpacks or to pull custom stacks, keyed by registry host. Each entry contains a username/password pair or a token. Custom stack credentials must be a username/password pair. CNB buildpack URLs must not embed credentials; use **data.credentials** instead. <br/>For private custom stacks, configure credentials on the app lifecycle for running instances and tasks.
9090
**data.stack** | _string_ | The root filesystem to use with the buildpack, for example `cflinuxfs4`. <br/>When the `diego_custom_stacks` feature flag is enabled, this can also be a container image URI with the `docker://` scheme. A custom stack requires custom buildpacks or admin buildpacks that are not associated with a stack. Its image layers count toward the app instance's disk quota.
9191

9292
During staging with a custom stack, Cloud Foundry uses the final repository component of the image URI, excluding its tag or digest, as `CNB_STACK_ID`. For example, `docker://registry.example.com/team/cflinuxfs4:1.268.0` sets `CNB_STACK_ID` to `cflinuxfs4`. The custom stack and buildpack must be compatible.

‎docs/v3/source/includes/resources/builds/_create.md.erb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Name | Type | Description
4040

4141
Name | Type | Description | Default
4242
---- | ---- | ----------- | -------
43-
**lifecycle** | [_lifecycle object_](#the-lifecycle-object) | Lifecycle information for a build | lifecycle on the app
43+
**lifecycle** | [_lifecycle object_](#the-lifecycle-object) | Lifecycle information for a build. <br/>Credentials supplied here apply only to staging and do not update the app's stored credentials. | lifecycle on the app
4444
**staging_memory_in_mb** | _integer_ | Memory in MB allocated for staging of the build
4545
**staging_disk_in_mb** | _integer_ | Disk space in MB allocated for staging of the build
4646
**staging_log_rate_limit_bytes_per_second** | _integer_ | Log rate limit in bytes per second allocated for staging of the build

‎lib/cloud_controller/diego/staging_action_builder.rb‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def cached_dependencies
4545
::Diego::Bbs::Models::CachedDependency.new(
4646
from: LifecycleBundleUriGenerator.uri(config.get(:diego, :lifecycle_bundles)[lifecycle_bundle_key]),
4747
to: '/tmp/lifecycle',
48-
cache_key: "#{@prefix}-#{lifecycle_stack}-lifecycle"
48+
cache_key: "#{@prefix}-#{lifecycle_bundle_stack}-lifecycle"
4949
)
5050
]
5151

@@ -94,7 +94,7 @@ def image_layers
9494

9595
layers = [
9696
::Diego::Bbs::Models::ImageLayer.new(
97-
name: "#{@prefix}-#{lifecycle_stack}-lifecycle",
97+
name: "#{@prefix}-#{lifecycle_bundle_stack}-lifecycle",
9898
url: LifecycleBundleUriGenerator.uri(config.get(:diego, :lifecycle_bundles)[lifecycle_bundle_key]),
9999
destination_path: '/tmp/lifecycle',
100100
layer_type: ::Diego::Bbs::Models::ImageLayer::Type::SHARED,
@@ -178,31 +178,33 @@ def download_actions
178178
end
179179

180180
def upload_actions
181-
actions = [
181+
[
182182
::Diego::Bbs::Models::UploadAction.new(
183183
user: 'vcap',
184184
artifact: 'droplet',
185185
from: '/tmp/droplet',
186186
to: upload_droplet_uri.to_s
187+
),
188+
189+
::Diego::Bbs::Models::UploadAction.new(
190+
user: 'vcap',
191+
artifact: 'build artifacts cache',
192+
from: @cache_source,
193+
to: upload_buildpack_artifacts_cache_uri.to_s
187194
)
188195
]
189-
190-
actions << ::Diego::Bbs::Models::UploadAction.new(
191-
user: 'vcap',
192-
artifact: 'build artifacts cache',
193-
from: @cache_source,
194-
to: upload_buildpack_artifacts_cache_uri.to_s
195-
)
196-
197-
actions
198196
end
199197

200198
def skip_detect?
201199
lifecycle_data[:buildpacks].any? { |buildpack| buildpack[:skip_detect] }
202200
end
203201

204202
def lifecycle_bundle_key
205-
:"#{@prefix}/#{LifecycleBundleStackResolver.resolve(lifecycle_stack)}"
203+
:"#{@prefix}/#{lifecycle_bundle_stack}"
204+
end
205+
206+
def lifecycle_bundle_stack
207+
LifecycleBundleStackResolver.resolve(lifecycle_stack)
206208
end
207209

208210
def upload_buildpack_artifacts_cache_uri

‎lib/utils/uri_utils.rb‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def parse_docker_uri(docker_uri)
112112
path = 'library/' + path if (official_docker_registry(name_parts[0]) || missing_registry(name_parts)) && path.exclude?('/')
113113
path, tag_digest = parse_docker_tag_digest_from_path(path)
114114

115-
raise InvalidDockerURI.new "Invalid image name [#{path}]" unless DOCKER_PATH_REGEX =~ path
115+
raise InvalidDockerURI.new "Invalid image name [#{path}]" unless valid_docker_path?(path)
116116
raise InvalidDockerURI.new "Invalid image tag [#{tag_digest}]" if tag_digest && DOCKER_TAG_DIGEST_REGEX !~ tag_digest
117117

118118
# if only sha256 presented, we add hash value as fragment to the uri,
@@ -128,6 +128,10 @@ def parse_docker_uri(docker_uri)
128128

129129
private
130130

131+
def valid_docker_path?(path)
132+
DOCKER_PATH_REGEX.match?(path) && path.split('/', -1).none?(&:empty?)
133+
end
134+
131135
def official_docker_registry(host)
132136
host == DOCKER_INDEX_SERVER
133137
end

‎spec/unit/actions/build_create_spec.rb‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,38 @@ module VCAP::CloudController
266266
end
267267
end
268268

269+
%w[buildpack cnb].each do |lifecycle_type|
270+
context "with #{lifecycle_type} build-only custom stack credentials" do
271+
let(:app) { create(:app_model, lifecycle_type.to_sym, space:) }
272+
let(:lifecycle) { LifecycleProvider.provide(package, staging_message) }
273+
let(:runtime_stack) { 'docker://registry.example.com/team/cflinuxfs4:runtime' }
274+
let(:build_credentials) { { 'registry.example.com' => { 'username' => 'build-user', 'password' => 'build-pass' } } }
275+
let(:request_lifecycle) { { type: lifecycle_type, data: lifecycle_data } }
276+
let(:lifecycle_data) do
277+
{
278+
stack: 'docker://registry.example.com/team/cflinuxfs4:staging',
279+
buildpacks: [buildpack_git_url],
280+
credentials: build_credentials
281+
}
282+
end
283+
284+
before do
285+
create(:feature_flag, name: 'diego_custom_stacks', enabled: true)
286+
app.lifecycle_data.update(stack: runtime_stack, credentials: nil)
287+
end
288+
289+
it 'uses the build credentials for staging without changing app credentials or stack' do
290+
action.create_and_stage(package:, lifecycle:)
291+
292+
expect(stager).to have_received(:stage) do |staging_details|
293+
expect(staging_details.lifecycle.staging_stack).to eq(lifecycle_data[:stack])
294+
expect(staging_details.lifecycle.credentials_for_staging.deep_stringify_keys).to eq(build_credentials)
295+
end
296+
expect(app.reload.lifecycle_data).to have_attributes(stack: runtime_stack, credentials: nil)
297+
end
298+
end
299+
end
300+
269301
describe 'creating a stage request' do
270302
it 'initiates a staging request' do
271303
build = action.create_and_stage(package:, lifecycle:)

‎spec/unit/lib/cloud_controller/diego/buildpack/staging_action_builder_spec.rb‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,6 @@ module Buildpack
152152
expect(upload_actions[1].upload_action).to eq(upload_build_artifacts_cache_action)
153153
end
154154

155-
context 'when using a custom stack' do
156-
let(:stack) { 'docker://registry.example.com/my-org/custom-stack:latest' }
157-
158-
it 'uploads the build artifacts cache' do
159-
upload_actions = builder.action.serial_action.actions[2].emit_progress_action.action.parallel_action.actions
160-
expect(upload_actions.map(&:upload_action)).to include(upload_build_artifacts_cache_action)
161-
end
162-
end
163-
164155
describe 'credhub' do
165156
let(:credhub_url) { TestConfig.config_instance.get(:credhub_api, :internal_url) }
166157
let(:expected_platform_options) do

‎spec/unit/lib/cloud_controller/diego/lifecycles/buildpack_lifecycle_data_validator_custom_stacks_spec.rb‎

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,12 @@
33

44
module VCAP::CloudController
55
RSpec.describe BuildpackLifecycleDataValidator do
6-
let(:stack) { create(:stack) }
76
let(:buildpack_url) { 'https://github.com/my-org/my-buildpack.git' }
87
let(:buildpack_info) { BuildpackInfo.new(buildpack_url, nil) }
98
let(:buildpack_infos) { [buildpack_info] }
109
let(:lifecycle_type) { Lifecycles::BUILDPACK }
1110

12-
subject(:validator) do
13-
BuildpackLifecycleDataValidator.new(
14-
buildpack_infos: buildpack_infos,
15-
stack: stack,
16-
stack_name: stack&.name,
17-
lifecycle_type: lifecycle_type
18-
)
19-
end
20-
2111
describe 'custom stacks' do
22-
let(:stack) { nil }
2312
let(:custom_stack_uri) { 'docker://docker.io/cloudfoundry/cflinuxfs4:1.268.0' }
2413

2514
subject(:validator) do
@@ -37,8 +26,6 @@ module VCAP::CloudController
3726
end
3827

3928
context 'with custom buildpacks (URL-based)' do
40-
let(:buildpack_info) { BuildpackInfo.new('https://github.com/my-org/my-buildpack.git', nil) }
41-
4229
it 'is valid' do
4330
expect(validator).to be_valid
4431
end
@@ -101,7 +88,7 @@ module VCAP::CloudController
10188
end
10289

10390
context 'with an invalid custom stack URI' do
104-
let(:custom_stack_uri) { 'docker://registry.example.com/' }
91+
let(:custom_stack_uri) { 'docker://registry.example.com/team/' }
10592

10693
it 'reports an invalid container image URI' do
10794
expect(validator).not_to be_valid
@@ -132,30 +119,5 @@ module VCAP::CloudController
132119
end
133120
end
134121
end
135-
136-
describe 'system stacks (unchanged behavior)' do
137-
context 'when stack exists in DB' do
138-
it 'is valid' do
139-
expect(validator).to be_valid
140-
end
141-
end
142-
143-
context 'when stack does not exist in DB' do
144-
let(:stack) { nil }
145-
146-
subject(:validator) do
147-
BuildpackLifecycleDataValidator.new(
148-
buildpack_infos: buildpack_infos,
149-
stack: nil,
150-
stack_name: 'nonexistent-stack'
151-
)
152-
end
153-
154-
it 'is not valid' do
155-
expect(validator).not_to be_valid
156-
expect(validator.errors[:stack]).to include('must be an existing stack')
157-
end
158-
end
159-
end
160122
end
161123
end

‎spec/unit/lib/cloud_controller/diego/staging_action_builder_spec.rb‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,53 @@ module Diego
88

99
let(:config) { Config.new({ staging: { legacy_md5_buildpack_paths_enabled: } }) }
1010

11+
%w[buildpack cnb].each do |prefix|
12+
context "with a custom stack and #{prefix} lifecycle assets" do
13+
subject(:builder) { described_class.new(config, nil, lifecycle_data, prefix, nil, nil, nil) }
14+
15+
let(:enable_declarative_asset_downloads) { false }
16+
let(:config) do
17+
Config.new({
18+
diego: {
19+
lifecycle_bundles: { "#{prefix}/#{Stack.default.name}": 'platform-bundle' },
20+
enable_declarative_asset_downloads: enable_declarative_asset_downloads
21+
}
22+
})
23+
end
24+
let(:lifecycle_data) do
25+
{
26+
stack: 'docker://registry.example.com/team/stack:v1',
27+
buildpacks: [],
28+
app_bits_checksum: { type: 'sha1' }
29+
}
30+
end
31+
32+
before do
33+
allow(LifecycleBundleUriGenerator).to receive(:uri).with('platform-bundle').and_return('http://lifecycle.example.com/bundle.tgz')
34+
end
35+
36+
it 'shares the platform lifecycle cache entry across custom image tags' do
37+
dependency = builder.cached_dependencies.first
38+
expect(dependency).to have_attributes(from: 'http://lifecycle.example.com/bundle.tgz', cache_key: "#{prefix}-#{Stack.default.name}-lifecycle")
39+
40+
lifecycle_data[:stack] = 'docker://registry.example.com/team/stack:v2'
41+
expect(builder.cached_dependencies.first).to eq(dependency)
42+
end
43+
44+
context 'with declarative asset downloads enabled' do
45+
let(:enable_declarative_asset_downloads) { true }
46+
47+
it 'names the lifecycle layer after the platform stack' do
48+
expect(builder.image_layers.first).to have_attributes(
49+
name: "#{prefix}-#{Stack.default.name}-lifecycle",
50+
url: 'http://lifecycle.example.com/bundle.tgz',
51+
destination_path: '/tmp/lifecycle'
52+
)
53+
end
54+
end
55+
end
56+
end
57+
1158
describe '#buildpack_path' do
1259
context 'when legacy_md5_buildpack_paths_enabled is false' do
1360
let(:legacy_md5_buildpack_paths_enabled) { false }

‎spec/unit/lib/utils/uri_utils_spec.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
expect(UriUtils.is_custom_stack_uri?('cflinuxfs4')).to be(false)
116116
expect(UriUtils.is_custom_stack_uri?('https://example.com/stack')).to be(false)
117117
expect(UriUtils.is_custom_stack_uri?('docker://')).to be(false)
118+
expect(UriUtils.is_custom_stack_uri?('docker://registry.example.com/team/')).to be(false)
118119
end
119120
end
120121

@@ -208,6 +209,12 @@
208209
expect { UriUtils.parse_docker_uri('publish/buildpack:?@sha256:e118d023acaee5cf13471ead39f68416ad6172ff0899f3257ce1481cd2b28a6a') }.to raise_error(UriUtils::InvalidDockerURI)
209210
expect { UriUtils.parse_docker_uri('publish/buildpack@sha256:e118d0') }.to raise_error(UriUtils::InvalidDockerURI)
210211
end
212+
213+
it 'rejects empty repository path components' do
214+
%w[registry.example.com/team/ registry.example.com//stack registry.example.com/team//stack].each do |image|
215+
expect { UriUtils.parse_docker_uri(image) }.to raise_error(UriUtils::InvalidDockerURI, /Invalid image name/)
216+
end
217+
end
211218
end
212219

213220
describe '.url_has_userinfo?' do

0 commit comments

Comments
 (0)