본문 바로가기
프로그래밍/golang

aws api signature 구현

by 나도한강뷰 2022. 11. 7.

어쩌다보니, aws api header의 authorization검증 부분을 공부하게 되었다.

request 내용을 보면, 크게 header, body부분으로 나누어져있고, header부분에 authorization부분이 있다.

authorization부분에는 access key와 해싱된 secret key(signature)부분이 있으며, aws api가 사용자 인증을 하기위해서는 access key와 secret key의 정합여부를 확인한다.

여기서 api를 받는 입장에서는 해싱된 secret key를 원래값으로 되돌릴 수 없기에, 내가 가지고 있는 secret key를 동일 방식으로 해싱하여서 두 값의 일치여부를 확인한다.

나는 여기서 api를 생성하는 tool은 terraform이라는 IaC tool을 사용하였고, 어떻게하면 동일한 해싱을 할 수 있는지 고민하였다.

참조 문서는 다음과 같다.

https://github.com/hashicorp/terraform

 

GitHub - hashicorp/terraform: Terraform enables you to safely and predictably create, change, and improve infrastructure. It is

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amon...

github.com

https://github.com/hashicorp/terraform-provider-aws

 

GitHub - hashicorp/terraform-provider-aws: Terraform AWS provider

Terraform AWS provider. Contribute to hashicorp/terraform-provider-aws development by creating an account on GitHub.

github.com

https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html

 

Accessing AWS using your AWS credentials - AWS General Reference

Accessing AWS using your AWS credentials AWS requires different types of security credentials, depending on how you access AWS and what type of AWS user you are. For example, you need a user name and password to sign in to the AWS Management Console, and y

docs.aws.amazon.com

https://github.com/aws/aws-sdk-go

 

GitHub - aws/aws-sdk-go: AWS SDK for the Go programming language.

AWS SDK for the Go programming language. Contribute to aws/aws-sdk-go development by creating an account on GitHub.

github.com

 

여기서 모든 구현을 얘기하기보다는 접근방식을 얘기하고싶다.

처음에는 aws api공식문서를 통해서 동일한 방식으로 해싱을 해보았다. 근데, 해싱자체보다는, 들어가는 input values가 다양하고, 정확히 무엇을 정의하고있는지 이해하는데 어려움을 겪었다.

그래서 input value들이 어떻게 정의되는지 궁금하여서, terraform의 opensource를 확인해 보았다. 다행히, terraform자체에서 aws api를 생성해내지않고, 내부적으로 grpc통신을 통하여서 aws-provider라는 놈에게 정보를 보내면, aws-provider가 api를 생성하는 것이였다.

api는 aws-sdk를 통해서 생성하고있었으며, connection과 session정보를 만들때, 정보안에 handler function으로 crendential관련 handler를 호출하고 있으며, connection과 session정보를 만들때 자동으로 생성되는것으로 확인하였다. 그래서 aws-sdk의 credential, v4부분을 확인하였고, 그부분을 이해하여서 동일해싱을 만들어내는 해싱함수를 만들었다.

 

여기서 aws-sdk를 그대로 쓰면 안되는가에 대한 의문점이 생기는데, aws-sdk는 body나 api를 만들 조건들을 넣어서 api를 생성하는데, 그에따라 시간정보같은 것들은 자동으로 생성되게된다. 하지만 시간정보에 따라서 해싱값이 달라지는점, 내가 원하는 api가 아닌 request에서 날아온 정보를 재조합해서 해싱을 해야된다는 점등이 영향을 주어서, sdk를 직접이용하는 방향이아닌, 관련 함수들을 나에게 맞도록 구성하여서 쓸 수있도록 만들었다.

 

누군가가 이것과 비슷한 구현을 하고싶다면, aws-sdk-go와 공식문서를 열심히 읽어보도록하자. 결국에 근본이 되는 부분이 가장 중요한것 같다.