@@ -53,23 +53,115 @@ run_tests();
5353
5454__DATA__
5555
56+ #
57+ #=== TEST 1: test GenerateDataKey with given Credentials
58+ #--- http_config eval: $::HttpConfig
59+ #--- config
60+ # error_log ../kms_test1_error.log debug;
61+ #
62+ # location /test-signature {
63+ # set $aws_access_key $TEST_NGINX_AWS_CLIENT_ID;
64+ # set $aws_secret_key $TEST_NGINX_AWS_SECRET;
65+ # set $aws_region us-east-1;
66+ # set $aws_service kms;
67+ #
68+ # content_by_lua '
69+ # local KmsService = require "api-gateway.aws.kms.KmsService"
70+ #
71+ # local service = KmsService:new({
72+ # aws_region = ngx.var.aws_region,
73+ # aws_secret_key = ngx.var.aws_secret_key,
74+ # aws_access_key = ngx.var.aws_access_key,
75+ # aws_debug = true, -- print warn level messages on the nginx logs
76+ # aws_conn_keepalive = 60000, -- how long to keep the sockets used for AWS alive
77+ # aws_conn_pool = 100 -- the connection pool size for sockets used to connect to AWS
78+ # })
79+ #
80+ # -- search for aliases
81+ # local list = service:listAliases()
82+ # assert(list ~= nil, "ListAliases should return at least 1 key")
83+ #
84+ # -- pick the first alias
85+ # local KeyId = list.Aliases[1].AliasName
86+ # ngx.say("KEY-ALIAS:" .. tostring(KeyId))
87+ #
88+ # -- generate a data key
89+ # local cipher = service:generateDataKey(KeyId, "AES_256")
90+ # local blob = cipher.CiphertextBlob
91+ # local blob_text = cipher.Plaintext
92+ # ngx.say("BLOB:" .. blob)
93+ #
94+ # local decoded = service:decrypt(blob)
95+ # if decoded.Plaintext ~= blob_text then
96+ # error( "KMS Error: [" .. blob_text .. "] does not match [" .. decoded.Plaintext .. "]" )
97+ # end
98+ #
99+ # -- encrypt a text
100+ # local encryptResult = service:encrypt(KeyId, blob_text)
101+ # local decryptResult = service:decrypt(encryptResult.CiphertextBlob)
102+ #
103+ # if decryptResult.Plaintext ~= blob_text then
104+ # error( "KMS Encrypt/Decrypt Error: [" .. blob_text .. "] does not match [" .. decryptResult.Plaintext .. "]" )
105+ # end
106+ # ';
107+ # }
108+ #
109+ #--- request
110+ #GET /test-signature?Action=GenerateDataKey
111+ #--- response_body_like eval
112+ #[".*KEY-ALIAS\\:.*BLOB\\:.*"]
113+ #--- error_code: 200
114+ #--- no_error_log
115+ #[error]
116+ #--- more_headers
117+ #X-Test: test
56118
57- === TEST 1: test GenerateDataKey with given Credentials
119+ # the next test is commented b/c you need IAM Credentials
120+ # to run it, connect to an EC2 node, and run curl http://169.254.169.254//latest/meta-data/iam/security-credentials/<iam-user >
121+ # then use AccessKeyId, SecretAccessKey and Token into the TEST command, like the following cmd :
122+ #
123+ # $ TEST_NGINX_AWS_CLIENT_ID="<AccessKeyId>"
124+ # TEST_NGINX_AWS_SECRET="<SecretAccessKey>" \
125+ # TEST_NGINX_AWS_TOKEN="<Token>" \
126+ # PATH=/usr/local/sbin:$PATH \
127+ # TEST_NGINX_SERVROOT=`pwd`/target/servroot \
128+ # TEST_NGINX_PORT=1989 \
129+ # prove -I ./test/resources/test-nginx/lib -r ./test/perl/kms.t
130+ #
131+ #
132+ === TEST 2 : test with IAM User. DO NOT PROVIDE ANY CREDENTIALS AND LET KMS FIGURE IT OUT AUTOMATICALLY USING IAM ROLES
58133-- - http_config eval: $::HttpConfig
59134-- - config
60- location /test-signature {
61- set $aws_access_key $TEST_NGINX_AWS_CLIENT_ID;
62- set $aws_secret_key $TEST_NGINX_AWS_SECRET;
135+ error_log .. / kms_test2_error. log debug;
136+
137+ location = /latest /meta-data/ iam/ security-credentials/ {
138+ return 200 ' test-iam-user' ;
139+ }
140+
141+ location = /latest /meta-data/ iam/ security-credentials/ test-iam-user {
142+ return 200 ' {
143+ "Code" : "Success",
144+ "LastUpdated" : "2014-11-03T01:56:20Z",
145+ "Type" : "AWS-HMAC",
146+ "AccessKeyId" : "$TEST_NGINX_AWS_CLIENT_ID",
147+ "SecretAccessKey" : "$TEST_NGINX_AWS_SECRET",
148+ "Token" : "$TEST_NGINX_AWS_SECURITY_TOKEN",
149+ "Expiration" : "2014-11-03T08:07:52Z"
150+ }' ;
151+ }
152+ location / test-with-iam {
153+ # set $aws_access_key $TEST_NGINX_AWS_CLIENT_ID;
154+ # set $aws_secret_key $TEST_NGINX_AWS_SECRET;
63155 set $ aws_region us-east-1;
64156 set $ aws_service kms;
65157
66158 content_by_lua '
67159 local KmsService = require "api-gateway.aws.kms.KmsService"
68160
69161 local service = KmsService:new({
162+ security_credentials_host = "127.0.0.1",
163+ security_credentials_port = $TEST_NGINX_PORT,
70164 aws_region = ngx.var.aws_region,
71- aws_secret_key = ngx.var.aws_secret_key,
72- aws_access_key = ngx.var.aws_access_key,
73165 aws_debug = true, -- print warn level messages on the nginx logs
74166 aws_conn_keepalive = 60000, -- how long to keep the sockets used for AWS alive
75167 aws_conn_pool = 100 -- the connection pool size for sockets used to connect to AWS
@@ -83,6 +175,9 @@ __DATA__
83175 local KeyId = list.Aliases[1].AliasName
84176 ngx.say("KEY-ALIAS:" .. tostring(KeyId))
85177
178+ local KeyId = "alias/GW-CACHE-MK"
179+ ngx.say("KEY ALIAS:" .. tostring(KeyId))
180+
86181 -- generate a data key
87182 local cipher = service:generateDataKey(KeyId, "AES_256")
88183 local blob = cipher.CiphertextBlob
@@ -103,105 +198,15 @@ __DATA__
103198 end
104199 ' ;
105200 }
106-
201+ -- - more_headers
202+ X -Test: test
107203-- - request
108- GET /test-signature?Action=GenerateDataKey
204+ GET / test-with-iam
109205-- - response_body_like eval
110- [".*KEY-ALIAS \\:.*BLOB\\:.*"]
206+ [" .*KEY\\ sALIAS \\ :.*BLOB\\ :.*" ]
111207-- - error_code: 200
112208-- - no_error_log
113209[error]
114- --- more_headers
115- X-Test: test
116-
117- # the next test is commented b/c you need IAM Credentials
118- # to run it, connect to an EC2 node, and run curl http://169.254.169.254//latest/meta-data/iam/security-credentials/<iam-user >
119- # then use AccessKeyId, SecretAccessKey and Token into the TEST command, like the following cmd :
120- #
121- # $ TEST_NGINX_AWS_CLIENT_ID="<AccessKeyId>"
122- # TEST_NGINX_AWS_SECRET="<SecretAccessKey>" \
123- # TEST_NGINX_AWS_TOKEN="<Token>" \
124- # PATH=/usr/local/sbin:$PATH \
125- # TEST_NGINX_SERVROOT=`pwd`/target/servroot \
126- # TEST_NGINX_PORT=1989 \
127- # prove -I ./test/resources/test-nginx/lib -r ./test/perl/kms.t
128- #
129- #
130- # === TEST 2: test with IAM User. DO NOT PROVIDE ANY CREDENTIALS AND LET KMS FIGURE IT OUT AUTOMATICALLY USING IAM ROLES
131- # --- http_config eval: $::HttpConfig
132- # --- config
133- # location = /latest/meta-data/iam/security-credentials/ {
134- # return 200 'test-iam-user';
135- # }
136- #
137- # location = /latest/meta-data/iam/security-credentials/test-iam-user {
138- # return 200 '{
139- # "Code" : "Success",
140- # "LastUpdated" : "2014-11-03T01:56:20Z",
141- # "Type" : "AWS-HMAC",
142- # "AccessKeyId" : "$TEST_NGINX_AWS_CLIENT_ID",
143- # "SecretAccessKey" : "$TEST_NGINX_AWS_SECRET",
144- # "Token" : "$TEST_NGINX_AWS_TOKEN",
145- # "Expiration" : "2014-11-03T08:07:52Z"
146- # }';
147- # }
148- # location /test-with-iam {
149- # #set $aws_access_key $TEST_NGINX_AWS_CLIENT_ID;
150- # #set $aws_secret_key $TEST_NGINX_AWS_SECRET;
151- # set $aws_region us-east-1;
152- # set $aws_service kms;
153- #
154- # content_by_lua '
155- # local KmsService = require "api-gateway.aws.kms.KmsService"
156- #
157- # local service = KmsService:new({
158- # security_credentials_host = "127.0.0.1",
159- # security_credentials_port = $TEST_NGINX_PORT,
160- # aws_region = ngx.var.aws_region,
161- # aws_debug = true, -- print warn level messages on the nginx logs
162- # aws_conn_keepalive = 60000, -- how long to keep the sockets used for AWS alive
163- # aws_conn_pool = 100 -- the connection pool size for sockets used to connect to AWS
164- # })
165- #
166- # -- search for aliases
167- # -- local list = service:listAliases()
168- # -- assert(list ~= nil, "ListAliases should return at least 1 key")
169- #
170- # -- pick the first alias
171- # -- local KeyId = list.Aliases[1].AliasName
172- #
173- # local KeyId = "alias/GW-CACHE-MK"
174- # ngx.say("KEY ALIAS:" .. tostring(KeyId))
175- #
176- # -- generate a data key
177- # local cipher = service:generateDataKey(KeyId, "AES_256")
178- # local blob = cipher.CiphertextBlob
179- # local blob_text = cipher.Plaintext
180- # ngx.say("BLOB:" .. blob)
181- #
182- # local decoded = service:decrypt(blob)
183- # if decoded.Plaintext ~= blob_text then
184- # error( "KMS Error: [" .. blob_text .. "] does not match [" .. decoded.Plaintext .. "]" )
185- # end
186- #
187- # -- encrypt a text
188- # local encryptResult = service:encrypt(KeyId, blob_text)
189- # local decryptResult = service:decrypt(encryptResult.CiphertextBlob)
190- #
191- # if decryptResult.Plaintext ~= blob_text then
192- # error( "KMS Encrypt/Decrypt Error: [" .. blob_text .. "] does not match [" .. decryptResult.Plaintext .. "]" )
193- # end
194- # ';
195- # }
196- # --- more_headers
197- # X-Test: test
198- # --- request
199- # GET /test-with-iam
200- # --- response_body_like eval
201- # [".*KEY\\sALIAS\\:.*BLOB\\:.*"]
202- # --- error_code: 200
203- # --- no_error_log
204- # [error]
205210
206211
207212
0 commit comments