azure-loganalytics-datacollector-api 0.1.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ff34cda0c8aa57ecc7b124abb610d846e1d7f123
4
- data.tar.gz: 9d52dd208fa123e83cfd0b6b60c2c8c60df3022a
2
+ SHA256:
3
+ metadata.gz: 8452aaabdb1e4cadea593892598d3618d72d4dc268b73612a8072b98bb144a29
4
+ data.tar.gz: c095ecb8502f5afcc742e1cfa7da6c5b4d4f59522dab871323761a45ba6f4af3
5
5
  SHA512:
6
- metadata.gz: a495b5e29998532540f558c9d9b98a3e503dd68d70e6ceda81072cec7e71f3fcbfb479c138f1d86a266809e0b7167ab6906ec0e2dfdaaf1939c716ce7b84be2f
7
- data.tar.gz: ca45575e8a867e9d00b5eab40ead381b20a4009c93aff12d9b000c0f50ddbd85103233b4033d29d8564d37aca70e35832e7301ef32d8ab61513c866b25091d91
6
+ metadata.gz: 710358fb3591f079e96ba0c4e26ba020ee5573dc3c27553883efa80dd62943a3dcde86c7e0dbb1d0ee5d1a20bdf28ea554be8fd2a7df64423088189492f07501
7
+ data.tar.gz: 523e5be5872c7450e8518835cf18e674043e5578e1b95588ea74070af40dce53418aaa455badeb2b156af9223e97228172bbb3af4e3ec8f4ea9ee2bef411eafd
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ # Personal test dir
11
+ /t/*
@@ -1,3 +1,18 @@
1
+ ## 0.3.0
2
+ * Enhance log type validation: check not only alpha but also numeric, underscore, and character length (may not exceed 100) - [issue #11](https://github.com/yokawasa/azure-log-analytics-data-collector/issues/11)
3
+
4
+ ## 0.2.0
5
+ * Support setting the x-ms-AzureResourceId Header - [issue #8](https://github.com/yokawasa/azure-log-analytics-data-collector/issues/8)
6
+
7
+ ## 0.1.6
8
+ * fix CVE-2020-8130 - [issue #7](https://github.com/yokawasa/azure-log-analytics-data-collector/issues/7)
9
+
10
+ ## 0.1.5
11
+ * Add endpoint parameter with the default value for Azure public - [PR#5](https://github.com/yokawasa/azure-log-analytics-data-collector/pull/5)
12
+
13
+ ## 0.1.4
14
+ * Add `set_proxy` method to client - [issue#3](https://github.com/yokawasa/azure-log-analytics-data-collector/issues/3)
15
+
1
16
  ## 0.1.2
2
17
  * fixedup bug - [issue #1](https://github.com/yokawasa/azure-log-analytics-data-collector/issues/1)
3
18
 
data/README.md CHANGED
@@ -1,11 +1,143 @@
1
1
  # azure-log-analytics-data-collector Ruby Client
2
- Azure Log Analytics Data Collector API Ruby Client
2
+
3
+ [Azure Log Analytics Data Collector API](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-collector-api) Client Libraries for Ruby. The repository was originally created for multiple programming languages, but it was refactored as a dedicated one for Ruby client. Python and PHP client libraries were moved to [azure-log-analytics-data-colloector-python](https://github.com/yokawasa/azure-log-analytics-data-collector-python) and [azure-log-analytics-data-colloector-php](https://github.com/yokawasa/azure-log-analytics-data-collector-php) respectively.
4
+
3
5
 
4
6
  ## Installation
5
- ```
7
+ ```bash
6
8
  gem install azure-loganalytics-datacollector-api
7
9
  ```
8
10
 
11
+ ## Sample code (Ruby Client)
12
+ ### Sample1 - No time_generated_field option
13
+ ```ruby
14
+ require "azure/loganalytics/datacollectorapi/client"
15
+
16
+ customer_id = '<Customer ID aka WorkspaceID String>'
17
+ shared_key = '<The primary or the secondary Connected Sources client authentication key>'
18
+ log_type = "MyCustomLog"
19
+
20
+ posting_records = []
21
+ record1= {
22
+ :string => "MyText1",
23
+ :boolean => true,
24
+ :number => 100
25
+ }
26
+ record2= {
27
+ :string => "MyText2",
28
+ :boolean => false,
29
+ :number => 200
30
+ }
31
+ posting_records.push(record1)
32
+ posting_records.push(record2)
33
+
34
+ client=Azure::Loganalytics::Datacollectorapi::Client::new( customer_id, shared_key)
35
+ res = client.post_data(log_type, posting_records)
36
+ puts res
37
+ puts "res code=#{res.code}"
38
+
39
+ if Azure::Loganalytics::Datacollectorapi::Client.is_success(res)
40
+ puts "operation was succeeded!"
41
+ else
42
+ puts "operation was failured!"
43
+ end
44
+ ```
45
+
46
+ ### Sample2 - With time_generated_field option
47
+ ```ruby
48
+ require "azure/loganalytics/datacollectorapi/client"
49
+
50
+ customer_id = '<Customer ID aka WorkspaceID String>'
51
+ shared_key = '<The primary or the secondary Connected Sources client authentication key>'
52
+ log_type = "MyCustomLog"
53
+
54
+ posting_records = []
55
+ record1= {
56
+ :string => "MyText1",
57
+ :boolean => true,
58
+ :number => 100,
59
+ :timegen => "2017-11-23T11:13:35.576Z" # YYYY-MM-DDThh:mm:ssZ
60
+ }
61
+ record2= {
62
+ :string => "MyText2",
63
+ :boolean => false,
64
+ :number => 200,
65
+ :timegen => "2017-11-23T12:13:35.576Z" # YYYY-MM-DDThh:mm:ssZ
66
+ }
67
+ posting_records.push(record1)
68
+ posting_records.push(record2)
69
+
70
+ time_generated_field = "timegen"
71
+ client=Azure::Loganalytics::Datacollectorapi::Client::new( customer_id, shared_key)
72
+ res = client.post_data(log_type, posting_records, time_generated_field)
73
+ puts res
74
+ puts "res code=#{res.code}"
75
+
76
+ if Azure::Loganalytics::Datacollectorapi::Client.is_success(res)
77
+ puts "operation was succeeded!"
78
+ else
79
+ puts "operation was failured!"
80
+ end
81
+ ```
82
+
83
+ ### Sample3 - With time_generated_field and azure_resource_id option
84
+ Supported setting azure_resource_id option from version [0.2.0](https://github.com/yokawasa/azure-log-analytics-data-collector/releases/tag/v0.2.0)
85
+ ```ruby
86
+ require "azure/loganalytics/datacollectorapi/client"
87
+
88
+ customer_id = '<Customer ID aka WorkspaceID String>'
89
+ shared_key = '<The primary or the secondary Connected Sources client authentication key>'
90
+ log_type = "MyCustomLog"
91
+
92
+ posting_records = []
93
+ record1= {
94
+ :string => "MyText1",
95
+ :boolean => true,
96
+ :number => 100,
97
+ :timegen => "2017-11-23T11:13:35.576Z" # YYYY-MM-DDThh:mm:ssZ
98
+ }
99
+ record2= {
100
+ :string => "MyText2",
101
+ :boolean => false,
102
+ :number => 200,
103
+ :timegen => "2017-11-23T12:13:35.576Z" # YYYY-MM-DDThh:mm:ssZ
104
+ }
105
+ posting_records.push(record1)
106
+ posting_records.push(record2)
107
+
108
+ time_generated_field = "timegen"
109
+
110
+ # Azure Resource ID
111
+ # [Azure Resource ID Format]
112
+ # /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}
113
+ azure_resource_id ="/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/otherResourceGroup/providers/Microsoft.Storage/storageAccounts/examplestorage"
114
+
115
+ client=Azure::Loganalytics::Datacollectorapi::Client::new( customer_id, shared_key)
116
+ res = client.post_data(log_type, posting_records, time_generated_field, azure_resource_id)
117
+ puts res
118
+ puts "res code=#{res.code}"
119
+
120
+ if Azure::Loganalytics::Datacollectorapi::Client.is_success(res)
121
+ puts "operation was succeeded!"
122
+ else
123
+ puts "operation was failured!"
124
+ end
125
+ ```
126
+
127
+ ### Sample4 - use proxy to access the API
128
+ ```ruby
129
+ require "azure/loganalytics/datacollectorapi/client"
130
+
131
+ ...
132
+ client=Azure::Loganalytics::Datacollectorapi::Client::new( customer_id, shared_key)
133
+ # client.set_proxy() # ENV['http_proxy'] is set by default
134
+ client.set_proxy(your_proxy)
135
+ res = client.post_data(log_type, posting_records, time_generated_field)
136
+ puts res
137
+ ...
138
+ ```
139
+
140
+
9
141
  ## Change log
10
142
 
11
143
  * [Changelog](ChangeLog.md)
@@ -28,4 +160,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/yokawa
28
160
  ## License
29
161
 
30
162
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
31
-
@@ -0,0 +1,12 @@
1
+ # Sample codes for azure-log-analytics-data-collector
2
+ [Azure Log Analytics HTTP Data Collecotr API](https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-data-collector-api) Client libraries
3
+
4
+ ## Client libraries and Sample code
5
+
6
+ | Language | Source Code | Project URL |
7
+ | ------------- | ------------- |------------- |
8
+ | Ruby | This repository | [Rubygem Package](https://rubygems.org/gems/azure-loganalytics-datacollector-api) |
9
+ | Python | [Source code](https://github.com/yokawasa/azure-log-analytics-data-collector-python) | [Python Package](https://pypi.python.org/pypi/azure-log-analytics-data-collector-api) |
10
+ | PHP | [Source code](https://github.com/yokawasa/azure-log-analytics-data-collector-php) | NONE |
11
+ | CSharp | [Sample Code](https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-data-collector-api#sample-requests) | NONE |
12
+ | PowerShell | [Sample Code](https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-data-collector-api#sample-requests) | NONE |
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "azure-loganalytics-datacollector-api"
8
8
  spec.version = Azure::Loganalytics::Datacollectorapi::VERSION
9
9
  spec.authors = ["Yoichi Kawasaki"]
10
- spec.email = ["yoichi.kawasaki@outlook.com"]
10
+ spec.email = ["yokawasa@gmail.com"]
11
11
  spec.summary = %q{Azure Log Analytics Data Collector API Ruby Client}
12
12
  spec.description = spec.summary
13
13
  spec.homepage = "https://github.com/yokawasa/azure-log-analytics-data-collector"
@@ -20,7 +20,6 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.add_dependency "rest-client"
23
- spec.add_development_dependency "bundler", "~> 1.13"
24
- spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rake", ">= 12.3.3"
25
24
  spec.add_development_dependency "rspec", "~> 3.0"
26
25
  end
@@ -8,7 +8,7 @@ module Azure
8
8
 
9
9
  class Client
10
10
 
11
- def initialize (customer_id, shared_key)
11
+ def initialize (customer_id, shared_key, endpoint ='ods.opinsights.azure.com')
12
12
  require 'rest-client'
13
13
  require 'json'
14
14
  require 'openssl'
@@ -17,15 +17,17 @@ module Azure
17
17
 
18
18
  @customer_id = customer_id
19
19
  @shared_key = shared_key
20
+ @endpoint = endpoint
21
+ @default_azure_resource_id = ''
20
22
  end
21
23
 
22
- def post_data(log_type, json_records, record_timestamp ='')
24
+ def post_data(log_type, json_records, record_timestamp ='', azure_resource_id ='' )
23
25
  raise ConfigError, 'no log_type' if log_type.empty?
24
- raise ConfigError, 'log_type must be only alpha characters' if not is_alpha(log_type)
26
+ raise ConfigError, 'log_type must only contain alpha numeric and _, and not exceed 100 chars' if not is_valid_log_type(log_type)
25
27
  raise ConfigError, 'no json_records' if json_records.empty?
26
28
  body = json_records.to_json
27
- uri = sprintf("https://%s.ods.opinsights.azure.com/api/logs?api-version=%s",
28
- @customer_id, API_VERSION)
29
+ uri = sprintf("https://%s.%s/api/logs?api-version=%s",
30
+ @customer_id, @endpoint, API_VERSION)
29
31
  date = rfc1123date()
30
32
  sig = signature(date, body.bytesize)
31
33
 
@@ -34,6 +36,7 @@ module Azure
34
36
  'Authorization' => sig,
35
37
  'Log-Type' => log_type,
36
38
  'x-ms-date' => date,
39
+ 'x-ms-AzureResourceId' => azure_resource_id.empty? ? @default_azure_resource_id : azure_resource_id,
37
40
  'time-generated-field' => record_timestamp
38
41
  }
39
42
 
@@ -41,14 +44,22 @@ module Azure
41
44
  res
42
45
  end
43
46
 
47
+ def set_proxy(proxy='')
48
+ RestClient.proxy = proxy.empty? ? ENV['http_proxy'] : proxy
49
+ end
50
+
51
+ def set_default_azure_resoruce_id(azure_resource_id)
52
+ @default_azure_resource_id = azure_resource_id
53
+ end
54
+
44
55
  def self.is_success(res)
45
56
  return (res.code == 200) ? true : false
46
57
  end
47
58
 
48
59
  private
49
60
 
50
- def is_alpha(s)
51
- return (s.match(/^[[:alpha:]]+$/)) ? true : false
61
+ def is_valid_log_type(s)
62
+ return ( s.match(/^[a-zA-Z0-9_]+$/) && s.length <= 100 ) ? true : false
52
63
  end
53
64
 
54
65
  def rfc1123date()
@@ -1,7 +1,7 @@
1
1
  module Azure
2
2
  module Loganalytics
3
3
  module Datacollectorapi
4
- VERSION = "0.1.2"
4
+ VERSION = "0.3.0"
5
5
  end
6
6
  end
7
7
  end
@@ -5,39 +5,46 @@ describe Azure::Loganalytics::Datacollectorapi::Client do
5
5
  expect(Azure::Loganalytics::Datacollectorapi::VERSION).not_to be nil
6
6
  end
7
7
 
8
- #it "does something useful" do
9
- # expect(false).to eq(true)
10
- #end
8
+ customer_id = '<Customer ID aka WorkspaceID String>'
9
+ shared_key = '<Primary Key String>'
10
+ log_type = "MyCustomLog"
11
11
 
12
- it "posting data to datacollector api" do
13
- customer_id = '<Customer ID aka WorkspaceID String>'
14
- shared_key = '<Primary Key String>'
15
- log_type = "ApacheAccessLog"
12
+ it "log type validation" do
13
+ json_records = []
14
+ dummy= {
15
+ :string => "dummy title",
16
+ :number => 10
17
+ }
18
+ valid_log_type = "abcedefghijklmnopqrstuvwxyz1234567890_"
19
+ invalid_log_type1 = "*-|[]//"
20
+ invalid_log_type2 = "abcde__xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
21
+ json_records.push(dummy)
22
+ client=Azure::Loganalytics::Datacollectorapi::Client::new( customer_id, shared_key)
16
23
 
24
+ res = client.post_data(valid_log_type, json_records)
25
+ expect(Azure::Loganalytics::Datacollectorapi::Client.is_success(res)).to eq(true)
26
+
27
+ expect{
28
+ client.post_data(invalid_log_type1, json_records)
29
+ }.to raise_error(Exception)
30
+
31
+ expect{
32
+ client.post_data(invalid_log_type2, json_records)
33
+ }.to raise_error(Exception)
34
+
35
+ end
36
+
37
+ it "posting data to datacollector api" do
17
38
  json_records = []
18
39
  record1= {
19
- :Log_ID => "5cdad72f-c848-4df0-8aaa-ffe033e75d57",
20
- :date => "2016-12-03 09:44:32 JST",
21
- :processing_time => "372",
22
- :remote => "101.202.74.59",
23
- :user => "-",
24
- :method => "GET / HTTP/1.1",
25
- :status => "304",
26
- :size => "-",
27
- :referer => "-",
28
- :agent => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:27.0) Gecko/20100101 Firefox/27.0"
40
+ :string => "MyText1",
41
+ :boolean => true,
42
+ :number => 100
29
43
  }
30
- record2 ={
31
- :Log_ID => "7260iswx-8034-4cc3-uirtx-f068dd4cd659",
32
- :date => "2016-12-03 09:45:14 JST",
33
- :processing_time => "105",
34
- :remote => "201.78.74.59",
35
- :user => "-",
36
- :method => "GET /manager/html HTTP/1.1",
37
- :status =>"200",
38
- :size => "-",
39
- :referer => "-",
40
- :agent => "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0"
44
+ record2= {
45
+ :string => "MyText2",
46
+ :boolean => false,
47
+ :number => 200
41
48
  }
42
49
  json_records.push(record1)
43
50
  json_records.push(record2)
@@ -47,4 +54,53 @@ describe Azure::Loganalytics::Datacollectorapi::Client do
47
54
  expect(Azure::Loganalytics::Datacollectorapi::Client.is_success(res)).to eq(true)
48
55
  end
49
56
 
57
+ it "posting data to datacollector api with time-generated-field" do
58
+ json_records = []
59
+ record1= {
60
+ :string => "MyText1",
61
+ :boolean => true,
62
+ :number => 100,
63
+ :timegen => "2020-05-05T11:13:35.576Z" # YYYY-MM-DDThh:mm:ssZ
64
+ }
65
+ record2= {
66
+ :string => "MyText2",
67
+ :boolean => false,
68
+ :number => 200,
69
+ :timegen => "2020-05-05T12:13:35.576Z" # YYYY-MM-DDThh:mm:ssZ
70
+ }
71
+ json_records.push(record1)
72
+ json_records.push(record2)
73
+
74
+ time_generated_field = "timegen"
75
+ client=Azure::Loganalytics::Datacollectorapi::Client::new( customer_id, shared_key)
76
+ res = client.post_data(log_type, json_records, time_generated_field)
77
+ expect(Azure::Loganalytics::Datacollectorapi::Client.is_success(res)).to eq(true)
78
+ end
79
+
80
+ it "posting data to datacollector api with azure-resource-id" do
81
+ json_records = []
82
+ record1= {
83
+ :string => "MyText1",
84
+ :boolean => true,
85
+ :number => 100
86
+ }
87
+ record2= {
88
+ :string => "MyText2",
89
+ :boolean => false,
90
+ :number => 200
91
+ }
92
+ json_records.push(record1)
93
+ json_records.push(record2)
94
+
95
+ time_generated_field = ""
96
+ # Azure Resource ID
97
+ # https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource#resourceid
98
+ # /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}
99
+ azure_resource_id ="/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/otherResourceGroup/providers/Microsoft.Storage/storageAccounts/examplestorage"
100
+
101
+ client=Azure::Loganalytics::Datacollectorapi::Client::new( customer_id, shared_key)
102
+ res = client.post_data(log_type, json_records, time_generated_field, azure_resource_id)
103
+ expect(Azure::Loganalytics::Datacollectorapi::Client.is_success(res)).to eq(true)
104
+ end
105
+
50
106
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: azure-loganalytics-datacollector-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoichi Kawasaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-29 00:00:00.000000000 Z
11
+ date: 2020-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -24,34 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.13'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.13'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rake
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
- - - "~>"
31
+ - - ">="
46
32
  - !ruby/object:Gem::Version
47
- version: '10.0'
33
+ version: 12.3.3
48
34
  type: :development
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
- - - "~>"
38
+ - - ">="
53
39
  - !ruby/object:Gem::Version
54
- version: '10.0'
40
+ version: 12.3.3
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: rspec
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -68,7 +54,7 @@ dependencies:
68
54
  version: '3.0'
69
55
  description: Azure Log Analytics Data Collector API Ruby Client
70
56
  email:
71
- - yoichi.kawasaki@outlook.com
57
+ - yokawasa@gmail.com
72
58
  executables:
73
59
  - console
74
60
  - setup
@@ -82,6 +68,7 @@ files:
82
68
  - LICENSE.txt
83
69
  - README.md
84
70
  - Rakefile
71
+ - SampleCodes.md
85
72
  - azure-loganalytics-datacollector-api.gemspec
86
73
  - bin/console
87
74
  - bin/setup
@@ -108,8 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  requirements: []
111
- rubyforge_project:
112
- rubygems_version: 2.6.2
98
+ rubygems_version: 3.1.3
113
99
  signing_key:
114
100
  specification_version: 4
115
101
  summary: Azure Log Analytics Data Collector API Ruby Client