bixby-agent 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.testguardrc +1 -0
  4. data/.travis.yml +25 -0
  5. data/Gemfile +61 -0
  6. data/Gemfile.lock +237 -0
  7. data/LICENSE +21 -0
  8. data/Rakefile +13 -0
  9. data/VERSION +1 -0
  10. data/bin/bixby-agent +15 -0
  11. data/bixby-agent.gemspec +186 -0
  12. data/etc/bixby-god.initd +70 -0
  13. data/etc/bixby.god +20 -0
  14. data/etc/god.d/bixby-agent.god +71 -0
  15. data/lib/bixby-agent.rb +16 -0
  16. data/lib/bixby-agent/agent.rb +98 -0
  17. data/lib/bixby-agent/agent/config.rb +109 -0
  18. data/lib/bixby-agent/agent/crypto.rb +73 -0
  19. data/lib/bixby-agent/agent/handshake.rb +81 -0
  20. data/lib/bixby-agent/agent/shell_exec.rb +111 -0
  21. data/lib/bixby-agent/agent_handler.rb +38 -0
  22. data/lib/bixby-agent/app.rb +208 -0
  23. data/lib/bixby-agent/app/cli.rb +112 -0
  24. data/lib/bixby-agent/config_exception.rb +5 -0
  25. data/lib/bixby-agent/help/system_time.rb +41 -0
  26. data/lib/bixby-agent/version.rb +8 -0
  27. data/lib/bixby-agent/websocket/client.rb +186 -0
  28. data/tasks/cane.rake +14 -0
  29. data/tasks/coverage.rake +2 -0
  30. data/tasks/coveralls.rake +11 -0
  31. data/tasks/jeweler.rake +21 -0
  32. data/tasks/test.rake +5 -0
  33. data/tasks/yard.rake +6 -0
  34. data/test/base.rb +92 -0
  35. data/test/helper.rb +29 -0
  36. data/test/stub_eventmachine.rb +38 -0
  37. data/test/support/root_dir/bixby.yml +8 -0
  38. data/test/support/root_dir/id_rsa +27 -0
  39. data/test/support/root_dir/server +27 -0
  40. data/test/support/root_dir/server.pub +9 -0
  41. data/test/support/test_bundle/bin/cat +2 -0
  42. data/test/support/test_bundle/bin/echo +2 -0
  43. data/test/support/test_bundle/digest +17 -0
  44. data/test/support/test_bundle/manifest.json +0 -0
  45. data/test/test_agent.rb +110 -0
  46. data/test/test_agent_exec.rb +53 -0
  47. data/test/test_agent_handler.rb +72 -0
  48. data/test/test_app.rb +138 -0
  49. data/test/test_bixby_common.rb +18 -0
  50. data/test/test_crypto.rb +78 -0
  51. data/test/websocket/test_client.rb +110 -0
  52. metadata +557 -0
@@ -0,0 +1,8 @@
1
+ ---
2
+ port: 18000
3
+ manager_uri: http://localhost:3000
4
+ uuid: 839b20d8aab74902bd4aa846567abe23
5
+ mac_address: 00:1f:5b:3d:6b:08
6
+ access_key: "12345"
7
+ secret_key: "my_voice_is_my_passport"
8
+ log_level: info
@@ -0,0 +1,27 @@
1
+ -----BEGIN RSA PRIVATE KEY-----
2
+ MIIEpAIBAAKCAQEA30PBCsqplS1HmJ91OQEGYuNyZw3Qf7WQ2eAvesPbVecXxBru
3
+ oRditkBeVMivLem2FK6TNao3ps+5gQR6N50pG2sRI+Itxepo2VVUhztxRwysPfED
4
+ 8pkCP0LS4uzF11Lb9VIHDdQTvgNmayqGYq5/PuiioKzDkIsHiR3V6FT/sXbGAMRi
5
+ pAmzMDjNa0OVZCbr8zXh76MPL+bVTJHvn0BysNhCdvn5tHSvHPw54y5WAbC5FEie
6
+ 5nySThSR0IsFWrOAiE+PPbmHy7YCC7ROIjJDvI9dfesmBn2KH2aesaP/6QLzZsft
7
+ iF/9D4hItQVRTk5yzXYeZFsC4be3sVyO4iOIiQIDAQABAoIBAQDMpoWHx9cEd9nd
8
+ 66R+2RKqiIQwNHh+58ZcAGRlKqydpFOjnXBJV29TUdNl8gKpmkYd1Rv5IAsdK+GF
9
+ gr4oxjWM/WpbsxJ0DXeIZ/AweWtPTi2AJbGRqXFm3XEDMzfyNPCMgWEX6uMh1GoI
10
+ J5XBVOQsnP+dOl+U2evRYvjVmzw5xMfM8GKWLj+qcAVyyzHrl4yTu/o6phRwdflh
11
+ 805+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iTpX0uOM5mtgggmHU3b
6
+ 2I+HhQQ1w4XTfivZbhzgB3hKtAALaMI5ycamCK+OMSjZU0YA7xcQbmYXZPgLT29Y
7
+ B11GSqjULOBqVyvJX/AREFQDiLU0SKLtx60G6NgBGPwHUvSBxOcAUzlBrtBek6OH
8
+ MQIDAQAB
9
+ -----END PUBLIC KEY-----
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ cat -
@@ -0,0 +1,2 @@
1
+ #!/bin/sh
2
+ echo $*
@@ -0,0 +1,17 @@
1
+ {
2
+ "digest": "c1af0e59a74367e83492a7501f6bdd7ed33de005c3f727a302c5ddfafa8c6f70",
3
+ "files": [
4
+ {
5
+ "file": "bin/cat",
6
+ "digest": "6d14ab63802f3ef51bdc16db1cfeb6df93167fe7a2aa09a6c737293d42c118b2"
7
+ },
8
+ {
9
+ "file": "bin/echo",
10
+ "digest": "5ca36fa0f9bd59e8169cc379215e6a481363df5b1e745f0442170fe6a56e1118"
11
+ },
12
+ {
13
+ "file": "manifest.json",
14
+ "digest": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
15
+ }
16
+ ]
17
+ }
File without changes
@@ -0,0 +1,110 @@
1
+
2
+ require 'helper'
3
+
4
+ module Bixby
5
+ module Test
6
+
7
+ class TestAgent < TestCase
8
+
9
+ def setup
10
+ super
11
+ ENV["BIXBY_HOME"] = nil
12
+ end
13
+
14
+ def teardown
15
+ super
16
+ ENV["BIXBY_HOME"] = nil
17
+ ENV["BIXBY_LOG"] = nil
18
+ end
19
+
20
+ def test_create_new_agent
21
+ @agent = create_new_agent()
22
+ @agent.save_config()
23
+ assert(@agent.new?)
24
+ assert( File.exists? File.join(@root_dir, "etc", "bixby.yml") )
25
+ assert ENV["BIXBY_HOME"]
26
+ assert_equal ENV["BIXBY_HOME"], @root_dir
27
+
28
+ assert_equal 1, Logging::Logger.root.level # default is info
29
+ end
30
+
31
+ def test_load_existing_agent
32
+ setup_existing_agent()
33
+ @agent = create_new_agent()
34
+ assert(!@agent.new?)
35
+ assert ENV["BIXBY_HOME"]
36
+ assert_equal ENV["BIXBY_HOME"], @root_dir
37
+
38
+ assert_equal 1, Logging::Logger.root.level # info level is in yaml
39
+ end
40
+
41
+ def test_load_existing_agent_override_log
42
+ ENV["BIXBY_LOG"] = "debug"
43
+ setup_existing_agent()
44
+ @agent = create_new_agent()
45
+ assert(!@agent.new?)
46
+ assert ENV["BIXBY_HOME"]
47
+ assert_equal ENV["BIXBY_HOME"], @root_dir
48
+
49
+ assert_equal 0, Logging::Logger.root.level
50
+ end
51
+
52
+ def test_load_existing_agent_using_env
53
+ setup_existing_agent()
54
+ ENV["BIXBY_HOME"] = @root_dir
55
+
56
+ @agent = Agent.create()
57
+ assert @agent
58
+ assert(!@agent.new?)
59
+ assert ENV["BIXBY_HOME"]
60
+ assert_equal ENV["BIXBY_HOME"], @root_dir
61
+ assert_equal @root_dir, Bixby.root
62
+ end
63
+
64
+ def test_register_with_manager
65
+ @agent = create_new_agent()
66
+
67
+ response_str = MultiJson.dump({
68
+ :data => {:server_key => "-----BEGIN RSA PUBLIC KEY-----",
69
+ :access_key => "foo",
70
+ :secret_key => "bar"},
71
+ :code => nil,
72
+ :status => "success",
73
+ :message => nil })
74
+
75
+ # stub out http request
76
+ stub_request(:post, "#{@manager_uri}/api").with { |req|
77
+ req.body =~ /inventory:register_agent/ and req.body =~ /"tenant":"pixelcop"/
78
+
79
+ }.to_return(:body => response_str, :status => 200)
80
+
81
+ response = @agent.register_agent(@manager_uri, @tenant, @password)
82
+ assert response.status == "success"
83
+
84
+ key_file = Bixby.path("etc", "server.pub")
85
+ assert File.exists? key_file
86
+ assert File.read(key_file).include? "PUBLIC KEY"
87
+
88
+ assert_equal "foo", @agent.access_key
89
+ assert_equal "bar", @agent.secret_key
90
+ end
91
+
92
+ def test_bad_config
93
+ setup_existing_agent()
94
+ File.open(config_file(), 'w') { |f| f.write("foo") }
95
+ assert_throws(SystemExit) do
96
+ Agent.create
97
+ end
98
+ end
99
+
100
+
101
+ private
102
+
103
+ def config_file
104
+ File.join(@root_dir, "etc", "bixby.yml")
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+ end
@@ -0,0 +1,53 @@
1
+
2
+ require 'helper'
3
+
4
+ module Bixby
5
+ module Test
6
+
7
+ class AgentExec < TestCase
8
+
9
+ def setup
10
+ super
11
+ setup_test_bundle("support", "test_bundle", "echo",
12
+ "c1af0e59a74367e83492a7501f6bdd7ed33de005c3f727a302c5ddfafa8c6f70")
13
+ @agent = create_new_agent()
14
+ end
15
+
16
+ def test_exec_error
17
+ # throws the first time
18
+ assert_throws(BundleNotFound) do
19
+ @agent.shell_exec(@c.to_hash)
20
+ end
21
+ end
22
+
23
+ def test_exec_pass
24
+ setup_root()
25
+ @c.args = "foo bar baz"
26
+ @c.user = `whoami`.strip
27
+ @c.group = `groups`.strip.split(/ /).first
28
+ ret = @agent.shell_exec(@c.to_hash)
29
+ assert ret
30
+ assert_kind_of CommandResponse, ret
31
+ assert_kind_of Fixnum, ret.status
32
+ assert_equal 0, ret.status
33
+ assert ret.stdout
34
+ assert ret.stderr
35
+ assert_equal "foo bar baz\n", ret.stdout
36
+ assert_equal "", ret.stderr
37
+ end
38
+
39
+ def test_execute_stdin
40
+ setup_root()
41
+ @c.command = "cat"
42
+ @c.stdin = "hi"
43
+ ret = @agent.shell_exec(@c.to_hash)
44
+ assert_equal 0, ret.status
45
+ assert_equal "hi", ret.stdout
46
+ assert_equal "", ret.stderr
47
+ end
48
+
49
+
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,72 @@
1
+
2
+ require 'helper'
3
+
4
+ module Bixby
5
+ module Test
6
+ class TestAgentHandler < TestCase
7
+
8
+ def setup
9
+ super
10
+ ENV["BIXBY_HOME"] = nil
11
+
12
+ setup_existing_agent()
13
+ @agent = create_new_agent()
14
+ assert(!@agent.new?)
15
+ assert ENV["BIXBY_HOME"]
16
+ assert_equal ENV["BIXBY_HOME"], @root_dir
17
+
18
+ # more setup
19
+ setup_test_bundle("support", "test_bundle", "echo",
20
+ "c1af0e59a74367e83492a7501f6bdd7ed33de005c3f727a302c5ddfafa8c6f70")
21
+ setup_root()
22
+ end
23
+
24
+ def teardown
25
+ ENV["BIXBY_HOME"] = nil
26
+ end
27
+
28
+ def test_handle
29
+ @c.args = "foobar"
30
+ req = JsonRequest.new("shell_exec", @c.to_hash)
31
+
32
+ res = AgentHandler.new(nil).handle(req) # this is the actual test call
33
+ assert res
34
+ assert_kind_of JsonResponse, res
35
+ assert res.success? # shell exec succeeded
36
+
37
+ res = CommandResponse.from_json_response(res)
38
+ assert_kind_of CommandResponse, res
39
+ assert res.success? # actual command succeeded
40
+ assert_equal 0, res.status
41
+ assert_equal "foobar\n", res.stdout
42
+ assert_equal "", res.stderr
43
+ end
44
+
45
+ def test_handle_bundle_not_found
46
+ @c.bundle = "foo"
47
+ req = JsonRequest.new("shell_exec", @c.to_hash)
48
+
49
+ res = AgentHandler.new(nil).handle(req)
50
+ puts res.to_s
51
+ refute res.success?
52
+ assert_equal 404, res.code
53
+ assert res.message =~ /bundle not found/
54
+ assert_equal nil, res.data
55
+ end
56
+
57
+ def test_handle_command_not_found
58
+ @c.command = "foo"
59
+ req = JsonRequest.new("shell_exec", @c.to_hash)
60
+
61
+ res = AgentHandler.new(nil).handle(req)
62
+ puts res.to_s
63
+ refute res.success?
64
+ assert_equal 404, res.code
65
+ assert res.message =~ /command not found/
66
+ assert_equal nil, res.data
67
+ end
68
+
69
+
70
+ end
71
+ end
72
+ end
data/test/test_app.rb ADDED
@@ -0,0 +1,138 @@
1
+
2
+ require 'helper'
3
+ require 'timeout'
4
+
5
+ module Bixby
6
+ module Test
7
+
8
+ class TestApp < TestCase
9
+
10
+ def teardown
11
+ ENV["BIXBY_LOG"] = nil
12
+ end
13
+
14
+ def test_load_agent
15
+ ARGV.clear
16
+ ARGV << "--register" << @manager_uri
17
+ ARGV << "-d" << @root_dir
18
+ ARGV << "--tenant" << "mytenant"
19
+ ARGV << "--password" << "mypass"
20
+
21
+ response_str = MultiJson.dump({
22
+ :data => {:server_key => "-----BEGIN RSA PUBLIC KEY-----",
23
+ :access_key => "foo",
24
+ :secret_key => "bar"},
25
+ :code => nil,
26
+ :status => "success",
27
+ :message => nil })
28
+
29
+ stub_request(:post, "http://localhost:3000/api").
30
+ to_return(:status => 200, :body => response_str)
31
+
32
+ # stub out daemons & sinatra server
33
+ Daemons.expects(:run_proc).once().with{ |name,opts|
34
+ name == "bixby-agent" && opts.kind_of?(Hash) &&
35
+ opts[:dir] == File.join(@root_dir, "var")
36
+ }
37
+
38
+ app = App.new.run!
39
+
40
+ assert_requested(:post, @manager_uri + "/api", :times => 1)
41
+ conf_file = File.join(@root_dir, "etc", "bixby.yml")
42
+ assert File.exists? conf_file
43
+ assert File.exists? File.join(@root_dir, "etc", "server.pub")
44
+
45
+ # verify yaml config file
46
+ conf = File.read(conf_file)
47
+ assert conf
48
+ assert_includes conf, "uuid"
49
+ assert_includes conf, "mac_address"
50
+ assert_includes conf, "access_key"
51
+ assert_includes conf, "secret_key"
52
+ assert_includes conf, "log_level"
53
+ Bixby::Agent::Config::KEYS.each{ |k| assert_includes conf, k }
54
+
55
+ assert_includes conf, "foo"
56
+ assert_includes conf, "bar"
57
+ assert_kind_of Hash, YAML.load(conf)
58
+ end
59
+
60
+ def test_run_agent
61
+
62
+ ARGV.clear
63
+ ARGV << "--register" # uri should default to bixby.io
64
+ ARGV << "--debug"
65
+ ARGV << "--directory" << @root_dir
66
+ ARGV << "--tenant" << "mytenant"
67
+ ARGV << "--password" << "mypass"
68
+
69
+ Bixby::WebSocket::Client.any_instance.expects(:start).once()
70
+
71
+ response_str = MultiJson.dump({
72
+ :data => {:server_key => "-----BEGIN RSA PUBLIC KEY-----",
73
+ :access_key => "foo",
74
+ :secret_key => "bar"},
75
+ :code => nil,
76
+ :status => "success",
77
+ :message => nil })
78
+
79
+ stub_request(:post, "https://bixby.io/api").
80
+ to_return(:status => 200, :body => response_str)
81
+
82
+ App.new.run!
83
+
84
+ assert_equal "debug", ENV["BIXBY_LOG"]
85
+ end
86
+
87
+ def test_run_with_bad_manager_uri
88
+ ARGV.clear
89
+ ARGV << "--register" << "asdf"
90
+ ARGV << "-d" << @root_dir
91
+
92
+ assert_throws(ConfigException) do
93
+ App.new.run!
94
+ end
95
+ end
96
+
97
+ def test_register_failed
98
+
99
+ ARGV.clear
100
+ ARGV << "-d"
101
+ ARGV << @root_dir
102
+ ARGV << @manager_uri
103
+
104
+ stub_request(:post, "http://localhost:3000/api").to_return(:status => 200, :body => MultiJson.dump({:status => "fail"}))
105
+
106
+ app = App.new
107
+ assert_throws(SystemExit) do
108
+ app.load_agent()
109
+ end
110
+
111
+ end
112
+
113
+ def test_register_agent_with_old_date
114
+
115
+ ARGV.clear
116
+ ARGV << "--register" # uri should default to bixby.io
117
+ ARGV << "--debug"
118
+ ARGV << "--directory" << @root_dir
119
+ ARGV << "--tenant" << "mytenant"
120
+ ARGV << "--password" << "mypass"
121
+
122
+ stub_request(:get, "http://google.com/").to_return(:status => 200, :body => "", :headers => {"Date" => (Time.new+1).utc.to_s})
123
+
124
+ ret = Bixby::JsonResponse.new("fail", "request is more than 900 seconds old", nil, 401).to_json
125
+ stub_request(:post, "https://bixby.io/api").
126
+ to_return(:status => 200, :body => ret)
127
+
128
+ assert_output(nil, /current time.*sudo ntpdate/m) do
129
+ assert_throws(SystemExit) do
130
+ App.new.run!
131
+ end
132
+ end
133
+ end
134
+
135
+ end
136
+
137
+ end
138
+ end