Skip to content

Commit 555cb86

Browse files
committed
Improve security by using temporary files and folders instead of .cuber
1 parent 5ed46f0 commit 555cb86

4 files changed

Lines changed: 26 additions & 14 deletions

File tree

lib/cuber/cli.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
require 'optparse'
2+
require 'tmpdir'
3+
require 'tempfile'
24
require 'fileutils'
35
require 'open3'
46
require 'erb'

lib/cuber/commands/deploy.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def initialize options
77
end
88

99
def execute
10+
mktmpdirs
1011
if @options[:release]
1112
print_step 'Deploying a past release'
1213
else
@@ -22,9 +23,21 @@ def execute
2223
configure
2324
apply
2425
rollout
26+
ensure
27+
rmtmpdirs
2528
end
2629

2730
private
31+
32+
def mktmpdirs
33+
@repo_tmpdir = Dir.mktmpdir
34+
@kubernetes_tmpdir = Dir.mktmpdir
35+
end
36+
37+
def rmtmpdirs
38+
FileUtils.remove_entry @repo_tmpdir
39+
FileUtils.remove_entry @kubernetes_tmpdir
40+
end
2841

2942
def print_step desc
3043
puts
@@ -33,17 +46,14 @@ def print_step desc
3346

3447
def checkout
3548
print_step 'Cloning Git repository'
36-
path = '.cuber/repo'
37-
FileUtils.mkdir_p path
38-
FileUtils.rm_rf path, secure: true
3949
cmd = ['git', 'clone']
4050
cmd += ['--branch', @options[:repo][:branch]] if @options[:repo][:branch]
41-
cmd += ['--depth', '1', @options[:repo][:url], path]
51+
cmd += ['--depth', '1', @options[:repo][:url], @repo_tmpdir]
4252
system(*cmd) || abort('Cuber: git clone failed')
4353
end
4454

4555
def commit_hash
46-
out, status = Open3.capture2 'git', 'rev-parse', '--short', 'HEAD', chdir: '.cuber/repo'
56+
out, status = Open3.capture2 'git', 'rev-parse', '--short', 'HEAD', chdir: @repo_tmpdir
4757
abort 'Cuber: cannot get commit hash' unless status.success?
4858
out.strip
4959
end
@@ -57,7 +67,7 @@ def pack
5767
tag = "#{@options[:image]}:#{@options[:release]}"
5868
cmd = ['pack', 'build', tag, '--builder', @options[:buildpacks], '--publish']
5969
cmd += ['--pull-policy', 'always', '--clear-cache'] if @options[:cache] == false
60-
system(*cmd, chdir: '.cuber/repo') || abort('Cuber: pack build failed')
70+
system(*cmd, chdir: @repo_tmpdir) || abort('Cuber: pack build failed')
6171
end
6272

6373
def build
@@ -67,7 +77,7 @@ def build
6777
cmd = ['docker', 'build']
6878
cmd += ['--pull', '--no-cache'] if @options[:cache] == false
6979
cmd += ['--platform', 'linux/amd64', '--progress', 'plain', '-f', dockerfile, '-t', tag, '.']
70-
system(*cmd, chdir: '.cuber/repo') || abort('Cuber: docker build failed')
80+
system(*cmd, chdir: @repo_tmpdir) || abort('Cuber: docker build failed')
7181
end
7282

7383
def push
@@ -80,13 +90,13 @@ def configure
8090
print_step 'Generating Kubernetes configuration'
8191
@options[:instance] = "#{@options[:app]}-#{Time.now.utc.iso8601.delete('^0-9')}"
8292
@options[:dockerconfigjson] = Base64.strict_encode64 File.read File.expand_path(@options[:dockerconfig] || '~/.docker/config.json')
83-
render 'deployment.yml', '.cuber/kubernetes/deployment.yml'
93+
render 'deployment.yml', File.join(@kubernetes_tmpdir, 'deployment.yml')
8494
end
8595

8696
def apply
8797
print_step 'Applying configuration to Kubernetes cluster'
8898
kubectl 'apply',
89-
'-f', '.cuber/kubernetes/deployment.yml',
99+
'-f', File.join(@kubernetes_tmpdir, 'deployment.yml'),
90100
'--prune', '-l', "app.kubernetes.io/name=#{@options[:app]},app.kubernetes.io/managed-by=cuber"
91101
end
92102

lib/cuber/commands/run.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ def command
3333

3434
def kubeexec command
3535
@options[:pod] = "pod-#{command.downcase.gsub(/[^a-z0-9]+/, '-')}-#{Time.now.utc.iso8601.delete('^0-9')}"
36-
path = ".cuber/kubernetes/#{@options[:pod]}.yml"
3736
full_command = command.shellsplit
3837
full_command.unshift 'launcher' unless @options[:buildpacks].to_s.strip.empty?
39-
render 'pod.yml', path
40-
kubectl 'apply', '-f', path
38+
Tempfile.create(['pod', '.yml']) do |temp|
39+
render 'pod.yml', temp.path
40+
kubectl 'apply', '-f', temp.path
41+
end
4142
kubectl 'wait', '--for', 'condition=ready', "pod/#{@options[:pod]}"
4243
kubectl 'exec', '-it', @options[:pod], '--', *full_command
4344
kubectl 'delete', 'pod', @options[:pod], '--wait=false'
44-
File.delete path
4545
end
4646

4747
end

lib/cuber/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Cuber
2-
VERSION = '1.13.0'.freeze
2+
VERSION = '1.14.0'.freeze
33
end

0 commit comments

Comments
 (0)