LoginSignup
1
2

More than 3 years have passed since last update.

github webhook で slack通知を行う簡易アプリをgoogle cloud function(python)に実装する際のsecretsの認証周りメモ

Posted at

背景

githubには発生したイベントをhookにリクエストを送信するwebhookという機能がある。例えば、github webhook + googlo cloud function + slack などを組み合わせると、独自のgithub -> slackの通知機能を実装することができる。

github secretにはsecrets設定ができる。これを使うことでよりセキュアなwebhookアプリケーションが作れる。

webhook のセキュリティ保護

今回は、google cloud function で通知アプリを実装する際に、このセキュア実装をどうやるかをメモ。

実装

import hmac
import hashlib

def verify_github_secrets(req) -> bool:
    secret_value = "YOUR_SECRET"
    sigExpected = request.headers.get("X-Hub-Signature").split('sha1=')[-1].strip()
    sigCalculated = hmac.new(secret_value.encode(), request.data, hashlib.sha1).hexdigest()
    return hmac.compare_digest(sigCalculated, sigExpected)

def main(req):
    if not verify_github_secrets(req):
        return "fail github auth"
    # TODO 実装
    return "ok"

参考

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2