Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Yamagoya で話せなかったところ/Fastly User Meetup #3

HAL
October 31, 2019

Yamagoya で話せなかったところ/Fastly User Meetup #3

HAL

October 31, 2019
Tweet

More Decks by HAL

Other Decks in Technology

Transcript

  1. お話すること • Fastly移行の状況 • Terraform の fastly ログ出力用 Google の認証情報のせいで

    毎回plan差分が出る • HSTS 設定しようとしたら http でも STSヘッダーが返る 3
  2. ヒット率/配信量 • 10/15 から1%振り分け • 10/28 に 100 % 振り分け

    • DNSのTTLは 300 • 期間中の設定変更は2回 ◦ Query Parameterの件で 一旦DNS振り分けを戻し た ◦ 2回目は Query Parameterのsortを入れた 7 1 % 10 % 50 % 100 %
  3. Query Parameterの扱い • GET で送られてくる文字列 ◦ キャッシュとして別のものとして扱われる ▪ 比率を上げたときにオリジンが耐えきれず死んでしまいかねない ◦

    Query Parameter を切り捨てるように設定 ◦ 反映速いのでこういう時助かります 8 If ( 条件 ) { set req.url = req.url.path; } Yamagoya 再掲
  4. Terraform fastly provider の話 • Terraform で Fastly の設定を行い、 ログ出力先を

    GCS に設 定 • gcslogging の secret_key の改行の扱いが原因で差分が出 ていた 12
  5. 移行で改善を図ったこと • 設定のコード化 ◦ Custom VCL という方法もあったが、一旦Terrafrom で管理することに ▪ Terraform

    provider のGoogle Credential の扱いに多少ハマる ▪ サブルーチンとパス単位でSnippet化 13 これのこと Yamagoya 再掲
  6. GCP JSON key { "type": "service_account", "project_id": "project-id", "private_key_id": "some_number",

    "private_key": "-----BEGIN PRIVATE KEY-----\n....=\n-----END PRIVATE KEY-----\n", "client_email": "<api-name>[email protected]", "client_id": "...", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/...<api-name>api%40project-id.iam.gserviceaccount.com" } 15 以下の client_email と private_key を環境変数に設定する private_key は改行が ¥n でエスケープされている
  7. 設定例 以下のような Terraform と CircleCI 設定を実施 16 gcslogging { #

    email and secret_key are form environment variable name = "<name>" bucket_name = "<bucket name>" path = "<path>" period = "3600" message_type = "blank" format = "<JSON format>" timestamp_format = "%Y-%m-%dT%H:%M:%S.000" } CircleCI環境変数 FASTLY_GCS_EMAIL <api-name>[email protected] FASTLY_GCS_SECRET_KEY -----BEGIN PRIVATE KEY-----\n....=\n-----END PRIVATE KEY-----\n
  8. Terraform plan差分 何も変更しなくても以下のterraform plan差分が発生 17 An execution plan has been

    generated and is shown below. Resource actions are indicated with the following symbols: ~ update in-place Terraform will perform the following actions: ~ fastly_service_v1.someservice gcslogging.1121940307.bucket_name: "<bukcetname>" => "" gcslogging.1121940307.email: "<api-name>[email protected]" => "" SNIP gcslogging.4265669124.bucket_name: "" => "<bukcetname>" gcslogging.4265669124.email: "" => "<api-name>[email protected]" SNIP Plan: 0 to add, 1 to change, 0 to destroy.
  9. Fastly Provider のドキュメント よく読むと改行入れて設定してとある (同じ挙動なのに Required と Optional の記述がずれてますね) 18

    Cloud Storage secret_key - (Required) The secret key associated with the target gcs bucket on your account. You may optionally provide this secret via an environment variable, FASTLY_GCS_SECRET_KEY. A typical format for the key is PEM format, containing actual newline characters where required. BigQuery secret_key - (Optional) The secret key associated with the sservice account that has write access to your BigQuery table. If not provided, this will be pulled from the FASTLY_BQ_SECRET_KEY environment variable. Typical format for this is a private key in a string with newlines.
  10. ワークアラウンド terraform plan 実行直前に ¥n を echo -e で改行文字にすればいいんじゃない? 19

    $ #!/bin/sh -eo pipefail export FASTLY_GCS_SECRET_KEY=$(echo -e $FASTLY_GCS_SECRET_KEY) terraform plan | ./tfnotify plan Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. SNIP No changes. Infrastructure is up-to-date. SNIP plan 差分でなくなりました ドキュメントはよく読みましょう
  11. HSTS • HTTP Strict Transport Security • RFC 6797 でPROPOSED

    STANDARDになっている • ブラウザに対して常にHTTPS経由でWebサイトに接続するよ う指示する機能 21
  12. HSTS 設定 • 移行ではなく新規で Fastly に収容し、HSTS を有効にする必 要が出た • Forcing

    TLS and enabling HSTS をそのまま使うと、http で も STS ヘッダーを返してしまう • これはRFC違反のようにも取れる • Snippet でリダイレクト設定とHSTS設定を書いてみた 22 An HSTS Host MUST NOT include the STS header field in HTTP responses conveyed over non-secure transport.
  13. やることは2つ • http アクセスの場合に https へ301リダイレクトする • https アクセスの場合に STS

    ヘッダーを付与する ◦ includeSubDomains, preload は一旦設定しない • 以下を参考に設定 ◦ https://support.fastly.com/hc/en-us/community/posts/360040169811-TLS- exception-for-specific-asset ◦ https://docs.fastly.com/en/guides/enabling-hsts-through-fastly 23
  14. snippet recv 24 snippet { name = "check protocol" type

    = "recv" priority = 100 content = <<EOS if (!req.http.Fastly-SSL) { error 601 "Force SSL"; } EOS }
  15. snippet cont. error 25 snippet { name = "redirect http

    to https" type = "error" priority = 100 content = <<EOS if (obj.status == 601) { set obj.status = 301; set obj.response = "Moved Permanently"; set obj.http.Location = "https://" req.http.host req.url; synthetic {""}; return (deliver); } EOS }
  16. snippet cont. deliver 26 snippet { name = "set sts

    header" type = "deliver" priority = 100 content = <<EOS if (req.http.Fastly-SSL) { set resp.http.Strict-Transport-Security = "max-age=31536000"; } EOS }