Golang github webhook 구현.

MINGKYME·2022년 5월 17일
0

개요

Go 언어를 이용하여, github webhook를 검증 및 command 실행을 한다.

사용 라이브러리

  • fiber
  • hmac
  • sha256
  • hex

github webhook API

github webhook을 등록 시, 3가지 입력 값을 필요로 한다.

Payload URL
-> Webhook URL

Content type
-> body에 대한 정보 구조를 선택한다.

  • json
  • x-www-form-urlencoded

Secret
-> 정상적인 요청인지 판별하기 위한 암호 키.

단순히 Secret 값을 전달해서 판별 시엔 패킷 스니핑 등의 공격에 의해 Secret key가 유출 될 수 있으므로, Body의 값을 Secret 을 hash로 사용하여 암호화 해서 비교한다.

이 때, Web Request 의 Header 중, X-Hub-Signature-256 에 암호화된 값이 존재한다.

Go 코드

func CheckSignature(c *fiber.Ctx) bool {
	hash := hmac.New(sha256.New, []byte("key"))
	hash.Write(c.Body())
	expectedStr := "sha256=" + hex.EncodeToString(hash.Sum(nil))
	if string(c.Request().Header.Peek("X-Hub-Signature-256")) == expectedStr {
		return true
	} else {
		return false
	}
}
profile
불편함을 해소하기 위해, 오늘도 디버깅을 합니다.

0개의 댓글