bosh-director 1.3202.0 → 1.3213.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/db/migrations/director/20151223172000_rename_requires_json.rb +7 -0
  3. data/db/migrations/director/20160106162749_runtime_configs.rb +19 -0
  4. data/db/migrations/director/20160106163433_add_runtime_configs_to_deployments.rb +7 -0
  5. data/db/migrations/director/20160202162216_add_post_start_completed_to_instance.rb +7 -0
  6. data/db/migrations/director/20160210201838_denormalize_compiled_package_stemcell_id_to_stemcell_name_and_version.rb +57 -0
  7. data/db/migrations/director/20160219175840_add_column_teams_to_deployments.rb +8 -0
  8. data/db/migrations/director/20160224222508_add_deployment_name_to_task.rb +7 -0
  9. data/db/migrations/director/20160225182206_rename_post_start_completed.rb +8 -0
  10. data/lib/bosh/director.rb +9 -0
  11. data/lib/bosh/director/api.rb +1 -1
  12. data/lib/bosh/director/api/api_helper.rb +27 -0
  13. data/lib/bosh/director/api/controllers/base_controller.rb +28 -5
  14. data/lib/bosh/director/api/controllers/cloud_configs_controller.rb +4 -3
  15. data/lib/bosh/director/api/controllers/deployments_controller.rb +165 -81
  16. data/lib/bosh/director/api/controllers/locks_controller.rb +1 -1
  17. data/lib/bosh/director/api/controllers/packages_controller.rb +4 -35
  18. data/lib/bosh/director/api/controllers/releases_controller.rb +6 -4
  19. data/lib/bosh/director/api/controllers/runtime_configs_controller.rb +41 -0
  20. data/lib/bosh/director/api/controllers/stemcells_controller.rb +1 -1
  21. data/lib/bosh/director/api/controllers/tasks_controller.rb +72 -5
  22. data/lib/bosh/director/api/deployment_manager.rb +10 -42
  23. data/lib/bosh/director/api/extensions/scoping.rb +11 -24
  24. data/lib/bosh/director/api/instance_lookup.rb +10 -22
  25. data/lib/bosh/director/api/instance_manager.rb +27 -15
  26. data/lib/bosh/director/api/local_identity_provider.rb +0 -8
  27. data/lib/bosh/director/api/problem_manager.rb +7 -19
  28. data/lib/bosh/director/api/property_manager.rb +12 -21
  29. data/lib/bosh/director/api/resurrector_manager.rb +4 -4
  30. data/lib/bosh/director/api/route_configuration.rb +1 -0
  31. data/lib/bosh/director/api/runtime_config_manager.rb +35 -0
  32. data/lib/bosh/director/api/snapshot_manager.rb +2 -2
  33. data/lib/bosh/director/api/task_helper.rb +2 -1
  34. data/lib/bosh/director/api/task_manager.rb +2 -8
  35. data/lib/bosh/director/api/uaa_identity_provider.rb +0 -16
  36. data/lib/bosh/director/blob_util.rb +3 -2
  37. data/lib/bosh/director/cloudcheck_helper.rb +17 -3
  38. data/lib/bosh/director/compile_task.rb +53 -24
  39. data/lib/bosh/director/compile_task_generator.rb +6 -6
  40. data/lib/bosh/director/compiled_package_group.rb +4 -3
  41. data/lib/bosh/director/compiled_release.rb +6 -0
  42. data/lib/bosh/director/compiled_release/manifest.rb +30 -0
  43. data/lib/bosh/director/compiled_release_manifest.rb +3 -3
  44. data/lib/bosh/director/config.rb +11 -1
  45. data/lib/bosh/director/deployment_plan.rb +1 -0
  46. data/lib/bosh/director/deployment_plan/assembler.rb +6 -2
  47. data/lib/bosh/director/deployment_plan/cloud_manifest_parser.rb +26 -10
  48. data/lib/bosh/director/deployment_plan/compilation_config.rb +43 -7
  49. data/lib/bosh/director/deployment_plan/compilation_instance_pool.rb +10 -3
  50. data/lib/bosh/director/deployment_plan/deployment_repo.rb +4 -10
  51. data/lib/bosh/director/deployment_plan/dynamic_network.rb +1 -1
  52. data/lib/bosh/director/deployment_plan/instance.rb +36 -17
  53. data/lib/bosh/director/deployment_plan/instance_plan.rb +13 -2
  54. data/lib/bosh/director/deployment_plan/instance_spec.rb +12 -4
  55. data/lib/bosh/director/deployment_plan/job.rb +73 -39
  56. data/lib/bosh/director/deployment_plan/job_availability_zone_parser.rb +4 -4
  57. data/lib/bosh/director/deployment_plan/job_migrator.rb +7 -7
  58. data/lib/bosh/director/deployment_plan/job_network_parser.rb +6 -6
  59. data/lib/bosh/director/deployment_plan/job_spec_parser.rb +91 -33
  60. data/lib/bosh/director/deployment_plan/links/link.rb +9 -4
  61. data/lib/bosh/director/deployment_plan/links/link_lookup.rb +23 -15
  62. data/lib/bosh/director/deployment_plan/links/link_path.rb +168 -15
  63. data/lib/bosh/director/deployment_plan/links/links_resolver.rb +34 -32
  64. data/lib/bosh/director/deployment_plan/links/template_link.rb +28 -8
  65. data/lib/bosh/director/deployment_plan/manifest_validator.rb +1 -1
  66. data/lib/bosh/director/deployment_plan/network_settings.rb +27 -13
  67. data/lib/bosh/director/deployment_plan/package_validator.rb +9 -5
  68. data/lib/bosh/director/deployment_plan/placement_planner/networks_to_static_ips.rb +4 -4
  69. data/lib/bosh/director/deployment_plan/planner.rb +31 -7
  70. data/lib/bosh/director/deployment_plan/planner_factory.rb +147 -6
  71. data/lib/bosh/director/deployment_plan/runtime_manifest_parser.rb +142 -0
  72. data/lib/bosh/director/deployment_plan/stemcell.rb +2 -2
  73. data/lib/bosh/director/deployment_plan/steps/package_compile_step.rb +3 -2
  74. data/lib/bosh/director/deployment_plan/template.rb +93 -8
  75. data/lib/bosh/director/deployment_plan/update_config.rb +10 -0
  76. data/lib/bosh/director/deployment_plan/vm_extension.rb +27 -0
  77. data/lib/bosh/director/errand/runner.rb +1 -1
  78. data/lib/bosh/director/errors.rb +11 -1
  79. data/lib/bosh/director/instance_updater.rb +46 -57
  80. data/lib/bosh/director/instance_updater/instance_state.rb +9 -0
  81. data/lib/bosh/director/instance_updater/state_applier.rb +18 -5
  82. data/lib/bosh/director/job_queue.rb +2 -2
  83. data/lib/bosh/director/job_renderer.rb +2 -2
  84. data/lib/bosh/director/job_updater.rb +7 -1
  85. data/lib/bosh/director/jobs/attach_disk.rb +2 -2
  86. data/lib/bosh/director/jobs/cloud_check/apply_resolutions.rb +6 -1
  87. data/lib/bosh/director/jobs/cloud_check/scan_and_fix.rb +14 -3
  88. data/lib/bosh/director/jobs/export_release.rb +1 -1
  89. data/lib/bosh/director/jobs/fetch_logs.rb +1 -4
  90. data/lib/bosh/director/jobs/helpers/compiled_package_deleter.rb +1 -2
  91. data/lib/bosh/director/jobs/helpers/stemcell_deleter.rb +0 -16
  92. data/lib/bosh/director/jobs/release/release_job.rb +7 -7
  93. data/lib/bosh/director/jobs/run_errand.rb +5 -5
  94. data/lib/bosh/director/jobs/ssh.rb +3 -3
  95. data/lib/bosh/director/jobs/update_deployment.rb +41 -5
  96. data/lib/bosh/director/jobs/update_release.rb +78 -82
  97. data/lib/bosh/director/jobs/update_stemcell.rb +1 -1
  98. data/lib/bosh/director/jobs/vm_state.rb +34 -21
  99. data/lib/bosh/director/key_generator.rb +54 -0
  100. data/lib/bosh/director/lock.rb +2 -2
  101. data/lib/bosh/director/log_bundles_cleaner.rb +1 -0
  102. data/lib/bosh/director/manifest/changeset.rb +39 -22
  103. data/lib/bosh/director/manifest/diff_lines.rb +1 -27
  104. data/lib/bosh/director/manifest/manifest.rb +22 -7
  105. data/lib/bosh/director/manifest/redactor.rb +44 -0
  106. data/lib/bosh/director/models.rb +1 -0
  107. data/lib/bosh/director/models/compiled_package.rb +21 -15
  108. data/lib/bosh/director/models/deployment.rb +10 -0
  109. data/lib/bosh/director/models/instance.rb +2 -1
  110. data/lib/bosh/director/models/release_version.rb +0 -16
  111. data/lib/bosh/director/models/runtime_config.rb +19 -0
  112. data/lib/bosh/director/models/template.rb +4 -4
  113. data/lib/bosh/director/package_dependencies_manager.rb +22 -0
  114. data/lib/bosh/director/password_helper.rb +18 -0
  115. data/lib/bosh/director/permission_authorizer.rb +50 -30
  116. data/lib/bosh/director/post_deployment_script_runner.rb +40 -0
  117. data/lib/bosh/director/problem_handlers/missing_disk.rb +2 -2
  118. data/lib/bosh/director/problem_resolver.rb +8 -2
  119. data/lib/bosh/director/problem_scanner/scanner.rb +1 -1
  120. data/lib/bosh/director/problem_scanner/vm_scan_stage.rb +1 -1
  121. data/lib/bosh/director/validation_helper.rb +5 -5
  122. data/lib/bosh/director/version.rb +1 -1
  123. data/lib/bosh/director/vm_creator.rb +8 -0
  124. data/lib/cloud/dummy.rb +1 -0
  125. metadata +51 -19
  126. data/lib/bosh/director/api/vm_state_manager.rb +0 -9
  127. data/lib/bosh/director/compiled_package/blob_sha_mismatch_error.rb +0 -5
  128. data/lib/bosh/director/compiled_package/compiled_package.rb +0 -30
@@ -6,7 +6,7 @@ module Bosh::Director
6
6
 
7
7
  locks = []
8
8
  lock_keys = redis.keys('lock:*')
9
- # Deliberatelly not using redis futures here as we expect that the number of lock keys will be very small
9
+ # Deliberately not using redis futures here as we expect that the number of lock keys will be very small
10
10
  lock_keys.each do |lock_key|
11
11
  lock_value = redis.get(lock_key)
12
12
  unless lock_value.nil?
@@ -34,9 +34,8 @@ module Bosh::Director
34
34
  end
35
35
 
36
36
  matching_packages = Models::Package.join(Models::CompiledPackage, :package_id=>:id)
37
- .select(:packages__name, :packages__fingerprint, :compiled_packages__dependency_key, :stemcells__operating_system, :stemcells__version)
38
- .join(Models::Stemcell, :id=>:stemcell_id)
39
- .where(fingerprint: fingerprint_list).all
37
+ .select(:packages__name, :packages__fingerprint, :compiled_packages__dependency_key, :stemcell_os, :stemcell_version)
38
+ .where(fingerprint: fingerprint_list).all
40
39
 
41
40
  matching_packages = filter_matching_packages(matching_packages, manifest)
42
41
 
@@ -45,45 +44,15 @@ module Bosh::Director
45
44
 
46
45
  # dependencies & stemcell should also match
47
46
  def filter_matching_packages(matching_packages, manifest)
47
+ compiled_release_manifest = CompiledRelease::Manifest.new(manifest)
48
48
  filtered_packages = []
49
-
50
49
  matching_packages.each do |package|
51
- stemcell_match = "#{package[:operating_system]}/#{package[:version]}" == compiled_package_meta(package.name, manifest)['stemcell']
52
- dependencies_match = package[:dependency_key] == dependency_key(package, manifest)
53
-
54
- if stemcell_match && dependencies_match
50
+ if compiled_release_manifest.has_matching_package(package.name, package[:stemcell_os], package[:stemcell_version], package[:dependency_key])
55
51
  filtered_packages << package
56
52
  end
57
53
  end
58
-
59
54
  filtered_packages
60
55
  end
61
-
62
- def dependency_key(package, manifest)
63
- compiled_package_meta = compiled_package_meta(package.name, manifest)
64
- dependencies = transitive_dependencies(compiled_package_meta, manifest)
65
-
66
- key = dependencies.to_a.sort_by {|k| k["name"]}.map { |p| [p['name'], p['version']]}
67
- Yajl::Encoder.encode(key)
68
- end
69
-
70
- def transitive_dependencies(compiled_package_meta, manifest)
71
- dependencies = Set.new
72
- return dependencies if compiled_package_meta['dependencies'].nil?
73
-
74
- compiled_package_meta['dependencies'].each do |dependency_package_name|
75
- dependency_compiled_package_meta = compiled_package_meta(dependency_package_name, manifest)
76
- dependencies << dependency_compiled_package_meta
77
- dependencies.merge(transitive_dependencies(dependency_compiled_package_meta, manifest))
78
- end
79
-
80
- dependencies
81
- end
82
-
83
- def compiled_package_meta(package_name, manifest)
84
- manifest['compiled_packages'].select { |p| p['name'] == package_name}[0]
85
- end
86
-
87
56
  end
88
57
  end
89
58
  end
@@ -24,7 +24,7 @@ module Bosh::Director
24
24
  redirect "/tasks/#{task.id}"
25
25
  end
26
26
 
27
- get '/', scope: :read do
27
+ get '/', scope: :read_releases do
28
28
  releases = @release_manager.get_all_releases
29
29
 
30
30
  json_encode(releases)
@@ -45,7 +45,7 @@ module Bosh::Director
45
45
  redirect "/tasks/#{task.id}"
46
46
  end
47
47
 
48
- get '/:name', scope: :read do
48
+ get '/:name', scope: :read_releases do
49
49
  name = params[:name].to_s.strip
50
50
 
51
51
  if params['version']
@@ -107,6 +107,8 @@ module Bosh::Director
107
107
  'blobstore_id' => template.blobstore_id,
108
108
  'sha1' => template.sha1,
109
109
  'fingerprint' => template.fingerprint.to_s,
110
+ 'consumes' => template.consumes,
111
+ 'provides' => template.provides
110
112
  }
111
113
  end
112
114
 
@@ -116,9 +118,9 @@ module Bosh::Director
116
118
  'blobstore_id' => package.blobstore_id,
117
119
  'sha1' => package.sha1,
118
120
  'fingerprint' => package.fingerprint.to_s,
119
- 'compiled_packages' => package.compiled_packages.sort_by { |cp| [cp.stemcell.name, cp.stemcell.version] }.map do |compiled|
121
+ 'compiled_packages' => package.compiled_packages.sort_by { |cp| [cp.stemcell_os, cp.stemcell_version] }.map do |compiled|
120
122
  {
121
- 'stemcell' => "#{compiled.stemcell.name}/#{compiled.stemcell.version}",
123
+ 'stemcell' => "#{compiled.stemcell_os}/#{compiled.stemcell_version}",
122
124
  'sha1' => compiled.sha1,
123
125
  'blobstore_id' => compiled.blobstore_id,
124
126
  }
@@ -0,0 +1,41 @@
1
+ require 'bosh/director/api/controllers/base_controller'
2
+
3
+ module Bosh::Director
4
+ module Api::Controllers
5
+ class RuntimeConfigsController < BaseController
6
+ post '/', :consumes => :yaml do
7
+ manifest_text = request.body.read
8
+ validate_manifest_yml(manifest_text)
9
+
10
+ Bosh::Director::Api::RuntimeConfigManager.new.update(manifest_text)
11
+ status(201)
12
+ end
13
+
14
+ get '/', scope: :read do
15
+ if params['limit'].nil? || params['limit'].empty?
16
+ status(400)
17
+ body("limit is required")
18
+ return
19
+ end
20
+
21
+ begin
22
+ limit = Integer(params['limit'])
23
+ rescue ArgumentError
24
+ status(400)
25
+ body("limit is invalid: '#{params['limit']}' is not an integer")
26
+ return
27
+ end
28
+
29
+ runtime_configs = Bosh::Director::Api::RuntimeConfigManager.new.list(limit)
30
+ json_encode(
31
+ runtime_configs.map do |runtime_config|
32
+ {
33
+ "properties" => runtime_config.properties,
34
+ "created_at" => runtime_config.created_at,
35
+ }
36
+ end
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
@@ -22,7 +22,7 @@ module Bosh::Director
22
22
  redirect "/tasks/#{task.id}"
23
23
  end
24
24
 
25
- get '/', scope: :read do
25
+ get '/', scope: :read_stemcells do
26
26
  stemcells = @stemcell_manager.find_all_stemcells
27
27
  json_encode(stemcells)
28
28
  end
@@ -3,7 +3,29 @@ require 'bosh/director/api/controllers/base_controller'
3
3
  module Bosh::Director
4
4
  module Api::Controllers
5
5
  class TasksController < BaseController
6
- get '/', scope: :read do
6
+
7
+ def initialize(config)
8
+ super(config)
9
+ @deployment_manager = Api::DeploymentManager.new
10
+ end
11
+
12
+ def self.authorization(perm)
13
+ return unless perm
14
+
15
+ condition do
16
+ type = params[:type]
17
+ task = @task_manager.find_task(params[:id])
18
+ if type == 'debug' || type == 'cpi' || !type
19
+ check_access_to_task(task, :admin)
20
+ elsif type == 'event' || type == 'result'
21
+ check_access_to_task(task, :read)
22
+ else
23
+ raise UnauthorizedToAccessDeployment, "Unknown type #{type}"
24
+ end
25
+ end
26
+ end
27
+
28
+ get '/', scope: :list_tasks do
7
29
  dataset = Models::Task.dataset
8
30
 
9
31
  if limit = params['limit']
@@ -34,20 +56,48 @@ module Bosh::Director
34
56
  ])
35
57
  end
36
58
 
37
- tasks = dataset.order_by(:timestamp.desc).map do |task|
59
+ deployment = params['deployment']
60
+ if deployment
61
+ dataset = dataset.filter(deployment_name: deployment)
62
+ deployment = @deployment_manager.find_by_name(deployment)
63
+ @permission_authorizer.granted_or_raise(deployment, :read, token_scopes)
64
+ end
65
+
66
+ tasks = dataset.order_by(:timestamp.desc).map
67
+
68
+ unless @permission_authorizer.is_granted?(:director, :read, token_scopes)
69
+ permitted_deployments = @deployment_manager.all_by_name_asc.select { |deployment|
70
+ @permission_authorizer.is_granted?(deployment, :read, token_scopes)
71
+ }.map { |deployment| deployment.name }
72
+
73
+ tasks = tasks.select do |task|
74
+ next false unless task.deployment_name
75
+ permitted_deployments.include?(task.deployment_name)
76
+ end
77
+ end
78
+
79
+ tasks = tasks.map do |task|
38
80
  if task_timeout?(task)
39
81
  task.state = :timeout
40
82
  task.save
41
83
  end
42
84
  @task_manager.task_to_hash(task)
43
85
  end
44
-
45
86
  content_type(:json)
46
87
  json_encode(tasks)
47
88
  end
48
89
 
49
- get '/:id', scope: :read do
90
+ get '/:id', scope: :list_tasks do
50
91
  task = @task_manager.find_task(params[:id])
92
+ deployment_name = task.deployment_name
93
+ if deployment_name
94
+ check_access_to_deployment(deployment_name, :read)
95
+ elsif !@permission_authorizer.is_granted?(:director, :read, token_scopes)
96
+ raise UnauthorizedToAccessDeployment,
97
+ 'One of the following scopes is required to access this task: ' +
98
+ @permission_authorizer.list_expected_scope(:director, :read, token_scopes).join(', ')
99
+ end
100
+
51
101
  if task_timeout?(task)
52
102
  task.state = :timeout
53
103
  task.save
@@ -60,7 +110,7 @@ module Bosh::Director
60
110
  # Sends back output of given task id and params[:type]
61
111
  # Example: `get /tasks/5/output?type=event` will send back the file
62
112
  # at /var/vcap/store/director/tasks/5/event
63
- get '/:id/output', scope: Api::Extensions::Scoping::ParamsScope.new(:type, {event: :read, result: :read}) do
113
+ get '/:id/output', authorization: :task_output, scope: :authorization do
64
114
  log_type = params[:type] || 'debug'
65
115
  task = @task_manager.find_task(params[:id])
66
116
 
@@ -79,6 +129,23 @@ module Bosh::Director
79
129
 
80
130
  private
81
131
 
132
+ def check_access_to_task(task, scope)
133
+ if task.deployment_name
134
+ check_access_to_deployment(task.deployment_name, scope)
135
+ else
136
+ @permission_authorizer.granted_or_raise(:director, scope, token_scopes)
137
+ end
138
+ end
139
+
140
+ def check_access_to_deployment(deployment_name, scope)
141
+ begin
142
+ deployment = @deployment_manager.find_by_name(deployment_name)
143
+ @permission_authorizer.granted_or_raise(deployment, scope, token_scopes)
144
+ rescue DeploymentNotFound
145
+ @permission_authorizer.granted_or_raise(:director, :admin, token_scopes)
146
+ end
147
+ end
148
+
82
149
  def task_timeout?(task)
83
150
  # Some of the old task entries might not have the checkpoint_time
84
151
  unless task.checkpoint_time
@@ -4,61 +4,29 @@ module Bosh::Director
4
4
  include ApiHelper
5
5
 
6
6
  def initialize
7
- @permission_authorizer = Bosh::Director::PermissionAuthorizer.new
7
+ @deployment_lookup = DeploymentLookup.new
8
8
  end
9
9
 
10
10
  def find_by_name(name)
11
- DeploymentLookup.new.by_name(name)
11
+ @deployment_lookup.by_name(name)
12
12
  end
13
13
 
14
- def find_available(token_scopes)
15
- deployments = Bosh::Director::Models::Deployment.order_by(:name.asc).all
16
- deployments.select do |deployment|
17
- @permission_authorizer.is_authorized?(deployment.scopes.split((',')), token_scopes)
18
- end
14
+ def all_by_name_asc
15
+ Bosh::Director::Models::Deployment.order_by(:name.asc).all
19
16
  end
20
17
 
21
- def create_deployment(username, deployment_manifest, cloud_config, options = {})
22
- random_name = "deployment-#{SecureRandom.uuid}"
23
- deployment_manifest_dir = Dir::tmpdir
24
- deployment_manifest_file = File.join(deployment_manifest_dir, random_name)
25
- unless check_available_disk_space(deployment_manifest_dir, deployment_manifest.size)
26
- raise NotEnoughDiskSpace, 'Uploading deployment manifest failed. ' +
27
- "Insufficient space on BOSH director in #{deployment_manifest_dir}"
28
- end
29
-
30
- write_file(deployment_manifest_file, deployment_manifest)
31
-
18
+ def create_deployment(username, deployment_manifest_file_path, cloud_config, runtime_config, deployment_name, options = {})
32
19
  cloud_config_id = cloud_config.nil? ? nil : cloud_config.id
33
- JobQueue.new.enqueue(username, Jobs::UpdateDeployment, 'create deployment', [deployment_manifest_file, cloud_config_id, options])
20
+ runtime_config_id = runtime_config.nil? ? nil : runtime_config.id
21
+ JobQueue.new.enqueue(username, Jobs::UpdateDeployment, 'create deployment', [deployment_manifest_file_path, cloud_config_id, runtime_config_id, options], deployment_name)
34
22
  end
35
23
 
36
24
  def delete_deployment(username, deployment, options = {})
37
- JobQueue.new.enqueue(username, Jobs::DeleteDeployment, "delete deployment #{deployment.name}", [deployment.name, options])
38
- end
39
-
40
- def deployment_to_json(deployment)
41
- result = {
42
- 'manifest' => deployment.manifest,
43
- }
44
-
45
- Yajl::Encoder.encode(result)
25
+ JobQueue.new.enqueue(username, Jobs::DeleteDeployment, "delete deployment #{deployment.name}", [deployment.name, options], deployment.name)
46
26
  end
47
27
 
48
- def deployment_instances_to_json(deployment)
49
- instances = []
50
- filters = {:deployment_id => deployment.id}
51
- Models::Instance.filter(filters).exclude(vm_cid: nil).each do |instance|
52
- instances << {
53
- 'agent_id' => instance.agent_id,
54
- 'cid' => instance.vm_cid,
55
- 'job' => instance.job,
56
- 'index' => instance.index,
57
- 'id' => instance.uuid
58
- }
59
- end
60
-
61
- Yajl::Encoder.encode(instances)
28
+ def deployment_instances_with_vms(deployment)
29
+ Models::Instance.where(deployment: deployment).exclude(vm_cid: nil)
62
30
  end
63
31
  end
64
32
  end
@@ -2,8 +2,6 @@ module Bosh::Director
2
2
  module Api
3
3
  module Extensions
4
4
  module Scoping
5
- ROUTES_WITH_EXTENDED_TIMEOUT = ['/stemcells', '/releases', '/restore']
6
-
7
5
  module Helpers
8
6
  def current_user
9
7
  @user.username if @user
@@ -15,11 +13,16 @@ module Bosh::Director
15
13
  end
16
14
 
17
15
  def self.registered(app)
18
- app.set default_scope: :write
16
+ app.set default_scope: :admin
19
17
  app.helpers(Helpers)
20
18
  end
21
19
 
22
20
  def scope(allowed_scope)
21
+ if allowed_scope == :authorization
22
+ # handled by the :authorization option of the route
23
+ return
24
+ end
25
+
23
26
  condition do
24
27
  if allowed_scope == :default
25
28
  scope = settings.default_scope
@@ -29,29 +32,13 @@ module Bosh::Director
29
32
  scope = allowed_scope
30
33
  end
31
34
 
32
- auth_provided = %w(HTTP_AUTHORIZATION X-HTTP_AUTHORIZATION X_HTTP_AUTHORIZATION).detect do |key|
33
- request.env.has_key?(key)
34
- end
35
-
36
- if auth_provided
37
- begin
38
- extended_token_timeout = ROUTES_WITH_EXTENDED_TIMEOUT.include?(request.path) &&
39
- request.media_type == mime_type(:multipart) &&
40
- request.request_method == 'POST'
41
-
42
- @user = identity_provider.get_user(request.env, extended_token_timeout: extended_token_timeout)
43
- rescue AuthenticationError
44
- end
45
- end
46
-
47
- if requires_authentication? && (@user.nil? || !identity_provider.valid_access?(@user, scope))
48
- response['WWW-Authenticate'] = 'Basic realm="BOSH Director"'
35
+ if requires_authentication?
49
36
  if @user.nil?
50
- message = "Not authorized: '#{request.path}'\n"
51
- else
52
- message = "Not authorized: '#{request.path}' requires one of the scopes: #{identity_provider.required_scopes(scope).join(", ")}\n"
37
+ # this should already be happening in base_controller#authentication
38
+ throw(:halt, [401, "Not authorized: '#{request.path}'\n"])
53
39
  end
54
- throw(:halt, [401, message])
40
+
41
+ @permission_authorizer.granted_or_raise(:director, scope, @user.scopes)
55
42
  end
56
43
  end
57
44
  end
@@ -11,39 +11,23 @@ module Bosh::Director
11
11
  instance
12
12
  end
13
13
 
14
- def by_attributes(deployment_name, job_name, job_index)
15
- deployment = DeploymentLookup.new.by_name(deployment_name)
16
-
14
+ def by_attributes(deployment, job_name, job_index)
17
15
  # Postgres cannot coerce an empty string to integer, and fails on Models::Instance.find
18
16
  job_index = nil if job_index.is_a?(String) && job_index.empty?
19
17
 
20
- filter = {
21
- deployment_id: deployment.id,
22
- job: job_name,
23
- index: job_index
24
- }
25
-
26
- instance = Models::Instance.find(filter)
18
+ instance = Models::Instance.find(deployment: deployment, job: job_name, index: job_index)
27
19
  if instance.nil?
28
20
  raise InstanceNotFound,
29
- "`#{deployment_name}/#{job_name}/#{job_index}' doesn't exist"
21
+ "`#{deployment.name}/#{job_name}/#{job_index}' doesn't exist"
30
22
  end
31
23
  instance
32
24
  end
33
25
 
34
- def by_uuid(deployment_name, job_name, uuid)
35
- deployment = DeploymentLookup.new.by_name(deployment_name)
36
-
37
- filter = {
38
- deployment_id: deployment.id,
39
- job: job_name,
40
- uuid: uuid
41
- }
42
-
43
- instance = Models::Instance.find(filter)
26
+ def by_uuid(deployment, job_name, uuid)
27
+ instance = Models::Instance.find(deployment: deployment, job: job_name, uuid: uuid)
44
28
  if instance.nil?
45
29
  raise InstanceNotFound,
46
- "`#{deployment_name}/#{job_name}/#{uuid}' doesn't exist"
30
+ "`#{deployment.name}/#{job_name}/#{uuid}' doesn't exist"
47
31
  end
48
32
  instance
49
33
  end
@@ -59,6 +43,10 @@ module Bosh::Director
59
43
  def find_all
60
44
  Models::Instance.all
61
45
  end
46
+
47
+ def by_deployment(deployment)
48
+ Models::Instance.filter(deployment: deployment).all
49
+ end
62
50
  end
63
51
  end
64
52
  end