Skip to content

Commit 44e4a58

Browse files
committed
Process run_action_user delegates docker_user to actual_droplet if exists
* Only test fallback logic to default docker users
1 parent 7344654 commit 44e4a58

File tree

2 files changed

+28
-108
lines changed

2 files changed

+28
-108
lines changed

app/models/runtime/process_model.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ def permitted_users
576576
end
577577

578578
def docker_run_action_user
579-
desired_droplet&.docker_user.presence || (Config.config.get(:allow_process_root_user) ? AppModel::DEFAULT_DOCKER_CONTAINER_USER : AppModel::DEFAULT_CONTAINER_USER)
579+
actual_droplet&.docker_user.presence || (Config.config.get(:allow_process_root_user) ? AppModel::DEFAULT_DOCKER_CONTAINER_USER : AppModel::DEFAULT_CONTAINER_USER)
580580
end
581581

582582
def non_unique_process_types

spec/unit/models/runtime/process_model_spec.rb

Lines changed: 27 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def act_as_cf_admin
678678
subject(:process) { ProcessModelFactory.make }
679679

680680
context 'when the process belongs to a Docker lifecycle app' do
681-
subject(:process) { ProcessModelFactory.make({ docker_image: 'example.com/image' }) }
681+
subject(:process) { ProcessModelFactory.make(:docker, { docker_image: 'example.com/image' }) }
682682
let(:droplet_execution_metadata) { '{"entrypoint":["/image-entrypoint.sh"],"user":"some-user"}' }
683683

684684
before do
@@ -687,56 +687,25 @@ def act_as_cf_admin
687687
process.desired_droplet.reload
688688
end
689689

690-
context 'when root user is allowed' do
690+
context 'when the process has a user specified' do
691691
before do
692-
TestConfig.override(allow_process_root_user: true)
693-
end
694-
695-
context 'when the process has a user specified' do
696-
before do
697-
process.update(user: 'ContainerUser')
698-
end
699-
700-
it 'returns the user' do
701-
expect(process.run_action_user).to eq('ContainerUser')
702-
end
703-
end
704-
705-
context 'when the droplet execution metadata specifies a user' do
706-
it 'returns the specified user' do
707-
expect(process.run_action_user).to eq('some-user')
708-
end
709-
end
710-
711-
context 'when the droplet execution metadata DOES NOT specify a user' do
712-
let(:droplet_execution_metadata) { '{"entrypoint":["/image-entrypoint.sh"]}' }
713-
714-
it 'returns the default "root" user' do
715-
expect(process.run_action_user).to eq('root')
716-
end
692+
process.update(user: 'ContainerUser')
717693
end
718694

719-
context 'when the droplet execution metadata is an empty string' do
720-
let(:droplet_execution_metadata) { '' }
721-
722-
it 'returns the default "root" user' do
723-
expect(process.run_action_user).to eq('root')
724-
end
695+
it 'returns the user' do
696+
expect(process.run_action_user).to eq('ContainerUser')
725697
end
698+
end
726699

727-
context 'when the droplet execution metadata is nil' do
728-
let(:droplet_execution_metadata) { nil }
729-
730-
it 'returns the default "root" user' do
731-
expect(process.run_action_user).to eq('root')
700+
context 'when the process DOES NOT have a user specified' do
701+
context 'when the process has a droplet' do
702+
before do
703+
# TODO: do we need to handle the distinction between desired and actual droplet?
704+
allow(process.actual_droplet).to(receive(:docker_user)).and_return('ActualDropletDockerUser')
732705
end
733-
end
734-
735-
context 'when the droplet execution metadata has invalid json' do
736-
let(:droplet_execution_metadata) { '{' }
737706

738-
it 'returns the default "root" user' do
739-
expect(process.run_action_user).to eq('root')
707+
it 'returns the docker_user from the desired_droplet' do
708+
expect(process.run_action_user).to eq('ActualDropletDockerUser')
740709
end
741710
end
742711

@@ -746,73 +715,24 @@ def act_as_cf_admin
746715
process.reload
747716
end
748717

749-
it 'returns the default "root" user' do
750-
expect(process.run_action_user).to eq('root')
751-
end
752-
end
753-
end
754-
755-
context 'when root user is not allowed' do
756-
before do
757-
TestConfig.override(allow_process_root_user: false)
758-
end
759-
760-
context 'when the process has a user specified' do
761-
before do
762-
process.update(user: 'ContainerUser')
763-
end
764-
765-
it 'returns the user' do
766-
expect(process.run_action_user).to eq('ContainerUser')
767-
end
768-
end
769-
770-
context 'when the droplet execution metadata specifies a user' do
771-
it 'returns the specified user' do
772-
expect(process.run_action_user).to eq('some-user')
773-
end
774-
end
775-
776-
context 'when the droplet execution metadata DOES NOT specify a user' do
777-
let(:droplet_execution_metadata) { '{"entrypoint":["/image-entrypoint.sh"]}' }
778-
779-
it 'returns the default "vcap" user' do
780-
expect(process.run_action_user).to eq('vcap')
781-
end
782-
end
783-
784-
context 'when the droplet execution metadata is an empty string' do
785-
let(:droplet_execution_metadata) { '' }
786-
787-
it 'returns the default "vcap" user' do
788-
expect(process.run_action_user).to eq('vcap')
789-
end
790-
end
791-
792-
context 'when the droplet execution metadata is nil' do
793-
let(:droplet_execution_metadata) { nil }
794-
795-
it 'returns the default "vcap" user' do
796-
expect(process.run_action_user).to eq('vcap')
797-
end
798-
end
799-
800-
context 'when the droplet execution metadata has invalid json' do
801-
let(:droplet_execution_metadata) { '{' }
718+
context 'when root user is allowed' do
719+
before do
720+
TestConfig.override(allow_process_root_user: true)
721+
end
802722

803-
it 'returns the default "vcap" user' do
804-
expect(process.run_action_user).to eq('vcap')
723+
it 'returns the default "root" user' do
724+
expect(process.run_action_user).to eq('root')
725+
end
805726
end
806-
end
807727

808-
context 'when the app does not have a droplet assigned' do
809-
before do
810-
process.app.update(droplet: nil)
811-
process.reload
812-
end
728+
context 'when root user IS NOT allowed' do
729+
before do
730+
TestConfig.override(allow_process_root_user: false)
731+
end
813732

814-
it 'returns the default "vcap" user' do
815-
expect(process.run_action_user).to eq('vcap')
733+
it 'returns the default "vcap" user' do
734+
expect(process.run_action_user).to eq('vcap')
735+
end
816736
end
817737
end
818738
end

0 commit comments

Comments
 (0)