github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/trace/trace_test.go (about)

     1  package trace_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/cf/trace"
     5  	. "github.com/onsi/ginkgo"
     6  	. "github.com/onsi/gomega"
     7  )
     8  
     9  var _ = Describe("trace", func() {
    10  	Describe("Sanitize", func() {
    11  		It("hides the authorization token header", func() {
    12  			request := `
    13  REQUEST:
    14  GET /v2/organizations HTTP/1.1
    15  Host: api.run.pivotal.io
    16  Accept: application/json
    17  Authorization: bearer eyJhbGciOiJSUzI1NiJ9.eyJqdGkiOiI3NDRkNWQ1My0xODkxLTQzZjktYjNiMy1mMTQxNDZkYzQ4ZmUiLCJzdWIiOiIzM2U3ZmVkNy1iMWMyLTRjMjAtOTU0My0yMTBiMjc2ODM1MDgiLCJzY29wZSI6WyJjbG91ZF9jb250cm9sbGVyLnJlYWQiLCJjbG91ZF9jb250cm9sbGVyLndyaXRlIiwib3BlbmlkIiwicGFzc3dvcmQud3JpdGUiXSwiY2xpZW50X2lkIjoiY2YiLCJjaWQiOiJjZiIsImdyYW50X3R5cGUiOiJwYXNzd29yZCIsInVzZXJfaWQiOiIzM2U3ZmVkNy1iMWMyLTRjMjAtOTU0My0yMTBiMjc2ODM1MDgiLCJ1c2VyX25hbWUiOiJtZ2VoYXJkK2NsaUBwaXZvdGFsbGFicy5jb20iLCJlbWFpbCI6Im1nZWhhcmQrY2xpQHBpdm90YWxsYWJzLmNvbSIsImlhdCI6MTM3ODI0NzgxNiwiZXhwIjoxMzc4MjkxMDE2LCJpc3MiOiJodHRwczovL3VhYS5ydW4ucGl2b3RhbC5pby9vYXV0aC90b2tlbiIsImF1ZCI6WyJvcGVuaWQiLCJjbG91ZF9jb250cm9sbGVyIiwicGFzc3dvcmQiXX0.LL_QLO0SztGRENmU-9KA2WouOyPkKVENGQoUtjqrGR-UIekXMClH6fmKELzHtB69z3n9x7_jYJbvv32D-dX1J7p1CMWIDLOzXUnIUDK7cU5Q2yuYszf4v5anKiJtrKWU0_Pg87cQTZ_lWXAhdsi-bhLVR_pITxehfz7DKChjC8gh-FiuDvH5qHxxPqYHUl9jPso5OQ0y0fqZpLt8Yq23DKWaFAZehLnrhFltdQ_jSLy1QAYYZVD_HpQDf9NozKXruIvXhyIuwGj99QmUs3LSyNWecy822VqOoBtPYS6CLegMuWWlO64TJNrnZuh5YsOuW8SudJONx2wwEqARysJIHw
    18  This is the body. Please don't get rid of me even though I contain Authorization: and some other text
    19  	`
    20  
    21  			expected := `
    22  REQUEST:
    23  GET /v2/organizations HTTP/1.1
    24  Host: api.run.pivotal.io
    25  Accept: application/json
    26  Authorization: [PRIVATE DATA HIDDEN]
    27  This is the body. Please don't get rid of me even though I contain Authorization: and some other text
    28  	`
    29  
    30  			Expect(Sanitize(request)).To(Equal(expected))
    31  		})
    32  
    33  		Describe("hiding passwords in the body of requests", func() {
    34  			It("hides passwords in query args", func() {
    35  				request := `
    36  POST /oauth/token HTTP/1.1
    37  Host: login.run.pivotal.io
    38  Accept: application/json
    39  Authorization: [PRIVATE DATA HIDDEN]
    40  Content-Type: application/x-www-form-urlencoded
    41  
    42  grant_type=password&password=password&scope=&username=mgehard%2Bcli%40pivotallabs.com
    43  `
    44  
    45  				expected := `
    46  POST /oauth/token HTTP/1.1
    47  Host: login.run.pivotal.io
    48  Accept: application/json
    49  Authorization: [PRIVATE DATA HIDDEN]
    50  Content-Type: application/x-www-form-urlencoded
    51  
    52  grant_type=password&password=[PRIVATE DATA HIDDEN]&scope=&username=mgehard%2Bcli%40pivotallabs.com
    53  `
    54  				Expect(Sanitize(request)).To(Equal(expected))
    55  			})
    56  
    57  			It("hides passwords in the JSON-formatted request body", func() {
    58  				request := `
    59  REQUEST: [2014-03-07T10:53:36-08:00]
    60  PUT /Users/user-guid-goes-here/password HTTP/1.1
    61  
    62  {"password":"stanleysPasswordIsCool","oldPassword":"stanleypassword!"}
    63  `
    64  
    65  				expected := `
    66  REQUEST: [2014-03-07T10:53:36-08:00]
    67  PUT /Users/user-guid-goes-here/password HTTP/1.1
    68  
    69  {"password":"[PRIVATE DATA HIDDEN]","oldPassword":"[PRIVATE DATA HIDDEN]"}
    70  `
    71  
    72  				Expect(Sanitize(request)).To(Equal(expected))
    73  			})
    74  
    75  			It("hides password containing \" in the JSON-formatted request body", func() {
    76  				request := `
    77  REQUEST: [2014-03-07T10:53:36-08:00]
    78  PUT /Users/user-guid-goes-here/password HTTP/1.1
    79  
    80  {"password":"stanleys\"PasswordIsCool","oldPassword":"stanleypassword!"}
    81  `
    82  
    83  				expected := `
    84  REQUEST: [2014-03-07T10:53:36-08:00]
    85  PUT /Users/user-guid-goes-here/password HTTP/1.1
    86  
    87  {"password":"[PRIVATE DATA HIDDEN]","oldPassword":"[PRIVATE DATA HIDDEN]"}
    88  `
    89  
    90  				Expect(Sanitize(request)).To(Equal(expected))
    91  			})
    92  
    93  			It("hides create-user passwords", func() {
    94  				request := `
    95  REQUEST: [2014-03-07T12:15:08-08:00]
    96  POST /Users HTTP/1.1
    97  {
    98  	"userName": "jiro",
    99  	"emails": [{"value":"jiro"}],
   100  	"password": "leansushi",
   101  	"name": {"givenName":"jiro", "familyName":"jiro"}
   102  }
   103  `
   104  				expected := `
   105  REQUEST: [2014-03-07T12:15:08-08:00]
   106  POST /Users HTTP/1.1
   107  {
   108  	"userName": "jiro",
   109  	"emails": [{"value":"jiro"}],
   110  	"password":"[PRIVATE DATA HIDDEN]",
   111  	"name": {"givenName":"jiro", "familyName":"jiro"}
   112  }
   113  `
   114  				Expect(Sanitize(request)).To(Equal(expected))
   115  			})
   116  		})
   117  
   118  		It("hides oauth tokens in the body of requests", func() {
   119  			response := `
   120  HTTP/1.1 200 OK
   121  Content-Length: 2132
   122  Cache-Control: no-cache
   123  Cache-Control: no-store
   124  Cache-Control: no-store
   125  Connection: keep-alive
   126  Content-Type: application/json;charset=UTF-8
   127  Date: Thu, 05 Sep 2013 16:31:43 GMT
   128  Expires: Thu, 01 Jan 1970 00:00:00 GMT
   129  Pragma: no-cache
   130  Pragma: no-cache
   131  Server: Apache-Coyote/1.1
   132  
   133  {"access_token":"eyJhbGciOiJSUzI1NiJ9.eyJqdGkiOiJjNmE3YzEzNi02NDk3LTRmYWYtODc5OS00YzQyZTFmM2M2ZjUiLCJzdWIiOiIzM2U3ZmVkNy1iMWMyLTRjMjAtOTU0My0yMTBiMjc2ODM1MDgiLCJzY29wZSI6WyJjbG91ZF9jb250cm9sbGVyLnJlYWQiLCJjbG91ZF9jb250cm9sbGVyLndyaXRlIiwib3BlbmlkIiwicGFzc3dvcmQud3JpdGUiXSwiY2xpZW50X2lkIjoiY2YiLCJjaWQiOiJjZiIsImdyYW50X3R5cGUiOiJwYXNzd29yZCIsInVzZXJfaWQiOiIzM2U3ZmVkNy1iMWMyLTRjMjAtOTU0My0yMTBiMjc2ODM1MDgiLCJ1c2VyX25hbWUiOiJtZ2VoYXJkK2NsaUBwaXZvdGFsbGFicy5jb20iLCJlbWFpbCI6Im1nZWhhcmQrY2xpQHBpdm90YWxsYWJzLmNvbSIsImlhdCI6MTM3ODM5ODcwMywiZXhwIjoxMzc4NDQxOTAzLCJpc3MiOiJodHRwczovL3VhYS5ydW4ucGl2b3RhbC5pby9vYXV0aC90b2tlbiIsImF1ZCI6WyJvcGVuaWQiLCJjbG91ZF9jb250cm9sbGVyIiwicGFzc3dvcmQiXX0.VZErs4AnXgAzEirSY1A0yV0xQItXiPqaMfpO__MBwCihEpMEtMKemvlUPn3HEKyOGINk9YzhPV30ILrBb0oPt9plCD42BLEtyr_cbeo-1zap6QuhN8YjAAKQgjNYKORSvgi9x13JrXtCGByviHVEBP39Zeum2ZoehZfClWS7YP9lUfqaIBWUDLLBQtT6AZRlbzLwH-MJ5GkH1DOkIXzuWBk0OXp4VNm38kxzLQMnOJ3aJTcWv3YBxJeIgasoQLadTPaEPLxDGeC7V6SqhGJdyyZVnGTOKLt5ict-fxDoX6CxFnT_ZuMvseSocPfS2Or0HR_FICHAv2_C_6yv_4aI7w","token_type":"bearer","refresh_token":"eyJhbGciOiJSUzI1NiJ9.eyJqdGkiOiJjMjM2M2E3Yi04M2MwLTRiN2ItYjg0Zi1mNTM3MTA4ZGExZmEiLCJzdWIiOiIzM2U3ZmVkNy1iMWMyLTRjMjAtOTU0My0yMTBiMjc2ODM1MDgiLCJzY29wZSI6WyJjbG91ZF9jb250cm9sbGVyLnJlYWQiLCJjbG91ZF9jb250cm9sbGVyLndyaXRlIiwib3BlbmlkIiwicGFzc3dvcmQud3JpdGUiXSwiaWF0IjoxMzc4Mzk4NzAzLCJleHAiOjEzODA5OTA3MDMsImNpZCI6ImNmIiwiaXNzIjoiaHR0cHM6Ly91YWEucnVuLnBpdm90YWwuaW8vb2F1dGgvdG9rZW4iLCJncmFudF90eXBlIjoicGFzc3dvcmQiLCJ1c2VyX25hbWUiOiJtZ2VoYXJkK2NsaUBwaXZvdGFsbGFicy5jb20iLCJhdWQiOlsiY2xvdWRfY29udHJvbGxlci5yZWFkIiwiY2xvdWRfY29udHJvbGxlci53cml0ZSIsIm9wZW5pZCIsInBhc3N3b3JkLndyaXRlIl19.G8K9hVy2TGvxWEHMmVT86iQ5szMjnN0pWog2ASawpDiV8A4QODn9lJQq0G08LjjElV6wKQywAxM6eU8p32byW6RU9Tu-0iz9lW96aWSppTjsb4itbPLxsdMXLSRKOow0vuuGhwaTYx9OZIMpzNbXJVwbRRyWlhty6LVrEZp3hG37HO-N7g2oJdFZwxATaE63iL5ZnikcvKrPkBTKUGZ8OIAvsAlHQiEnbB8mfaw6Bh74ciTjOl0DYbHlZoEMQazXkLnY3INgCyErRcjtNkjRQGe6fOV4v1Wx3PAZ05gaBsAOaThgifz4Rmaf--hnrhtYI5F3g17tDmht6udZv1_C6A","expires_in":43199,"scope":"cloud_controller.read cloud_controller.write openid password.write","jti":"c6a7c136-6497-4faf-8799-4c42e1f3c6f5"}
   134  `
   135  
   136  			expected := `
   137  HTTP/1.1 200 OK
   138  Content-Length: 2132
   139  Cache-Control: no-cache
   140  Cache-Control: no-store
   141  Cache-Control: no-store
   142  Connection: keep-alive
   143  Content-Type: application/json;charset=UTF-8
   144  Date: Thu, 05 Sep 2013 16:31:43 GMT
   145  Expires: Thu, 01 Jan 1970 00:00:00 GMT
   146  Pragma: no-cache
   147  Pragma: no-cache
   148  Server: Apache-Coyote/1.1
   149  
   150  {"access_token":"[PRIVATE DATA HIDDEN]","token_type":"[PRIVATE DATA HIDDEN]","refresh_token":"[PRIVATE DATA HIDDEN]","expires_in":43199,"scope":"cloud_controller.read cloud_controller.write openid password.write","jti":"c6a7c136-6497-4faf-8799-4c42e1f3c6f5"}
   151  `
   152  
   153  			Expect(Sanitize(response)).To(Equal(expected))
   154  		})
   155  
   156  		It("hides service auth tokens in the request body", func() {
   157  			response := `
   158  HTTP/1.1 200 OK
   159  Content-Length: 2132
   160  Cache-Control: no-cache
   161  Cache-Control: no-store
   162  Cache-Control: no-store
   163  Connection: keep-alive
   164  Content-Type: application/json;charset=UTF-8
   165  Date: Thu, 05 Sep 2013 16:31:43 GMT
   166  Expires: Thu, 01 Jan 1970 00:00:00 GMT
   167  Pragma: no-cache
   168  Pragma: no-cache
   169  Server: Apache-Coyote/1.1
   170  
   171  {"label":"some label","provider":"some provider","token":"some-token-with-stuff-in-it"}
   172  `
   173  
   174  			expected := `
   175  HTTP/1.1 200 OK
   176  Content-Length: 2132
   177  Cache-Control: no-cache
   178  Cache-Control: no-store
   179  Cache-Control: no-store
   180  Connection: keep-alive
   181  Content-Type: application/json;charset=UTF-8
   182  Date: Thu, 05 Sep 2013 16:31:43 GMT
   183  Expires: Thu, 01 Jan 1970 00:00:00 GMT
   184  Pragma: no-cache
   185  Pragma: no-cache
   186  Server: Apache-Coyote/1.1
   187  
   188  {"label":"some label","provider":"some provider","token":"[PRIVATE DATA HIDDEN]"}
   189  `
   190  
   191  			Expect(Sanitize(response)).To(Equal(expected))
   192  		})
   193  
   194  		Describe("hiding credentials in application environment variables", func() {
   195  			It("hides the value of any key matching case-insensitive substring 'token'", func() {
   196  				response := `
   197  HTTP/1.1 200 OK
   198  Content-Type: application/json;charset=utf-8
   199  
   200  {"guid":"99fefc8e-845e-47f3-a8b1-26e8a00222d9","name":"example","environment_json":{"token":"mytoken","TOKEN":"mytoken","foo_token_bar":"mytoken","FOO_TOKEN_BAR":"mytoken"},"memory":1024,"instances":1}
   201  `
   202  
   203  				expected := `
   204  HTTP/1.1 200 OK
   205  Content-Type: application/json;charset=utf-8
   206  
   207  {"guid":"99fefc8e-845e-47f3-a8b1-26e8a00222d9","name":"example","environment_json":{"token":"[PRIVATE DATA HIDDEN]","TOKEN":"[PRIVATE DATA HIDDEN]","foo_token_bar":"[PRIVATE DATA HIDDEN]","FOO_TOKEN_BAR":"[PRIVATE DATA HIDDEN]"},"memory":1024,"instances":1}
   208  `
   209  
   210  				Expect(Sanitize(response)).To(Equal(expected))
   211  			})
   212  
   213  			It("hides the value of any key matching case-insensitive substring 'password'", func() {
   214  				response := `
   215  HTTP/1.1 200 OK
   216  Content-Type: application/json;charset=utf-8
   217  
   218  {"guid":"99fefc8e-845e-47f3-a8b1-26e8a00222d9","name":"example","environment_json":{"password":"mypass","PASSWORD":"mypass","foo_password_bar":"mypass","FOO_PASSWORD_BAR":"mypass"},"memory":1024,"instances":1}
   219  `
   220  
   221  				expected := `
   222  HTTP/1.1 200 OK
   223  Content-Type: application/json;charset=utf-8
   224  
   225  {"guid":"99fefc8e-845e-47f3-a8b1-26e8a00222d9","name":"example","environment_json":{"password":"[PRIVATE DATA HIDDEN]","PASSWORD":"[PRIVATE DATA HIDDEN]","foo_password_bar":"[PRIVATE DATA HIDDEN]","FOO_PASSWORD_BAR":"[PRIVATE DATA HIDDEN]"},"memory":1024,"instances":1}
   226  `
   227  
   228  				Expect(Sanitize(response)).To(Equal(expected))
   229  			})
   230  		})
   231  	})
   232  })