4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

dockerでazure functionsのhttpTriggerをfunctionのauthLevelで開発できるようにする

Posted at

##背景

  • local開発環境はdocker使ってazure functions構成のnodejsのapiを開発している。
  • 認証としてはfunctionのauthLevelを使いたい。
  • authLevelの設定はこんな感じ
{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "your/route",
      "methods": ["post"]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "scriptFile": "../your/source/folder/index.js"
}
  • API_KEYはazure function appのポータルからとるか、az cliで取れたりする。
  • 使う時はこんな感じでcodeというパラメータを使う:
https://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>?code=<API_KEY>
  • あるいはrequestのheadersにx-functions-keyを追加することで認証ができる。

課題

  • Localのテストで、
    開発環境のdocker hosted azure functionsに認証を通すには、
    API_KEYを手動で設定する必要がある。(自動生成したkeyは知らないし)

解決方法

*まずは下記ファイルを作成:

host_local_secrets.json

{
    "masterKey": {
        "name": "master",
        "value": "your_local_test_master_key",
        "encrypted": false
    },
    "functionKeys": [{
        "name": "default",
        "value": "your_local_test_function_key",
        "encrypted": false
    }]
}
  • Dockerfileに下記を追加:

Dockerfile

FROM mcr.microsoft.com/azure-functions/node:3.0-node12
RUN mkdir /etc/secrets/
ENV FUNCTIONS_SECRETS_PATH=/etc/secrets
ENV AzureWebJobsSecretStorageType=Files
ADD host_local_secrets.json /etc/secrets/host.json
  • Boom!!! Done. 認証ができるようになった。
  • Fly as you wish. To the moon. Freely.
4
0
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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?