LoginSignup
1
1

More than 3 years have passed since last update.

chromedriver/seleniumコンテナで.pfxキー認証が必要なサイトにアクセスできるdockerfileを書く

Last updated at Posted at 2019-11-29

RでWEBスクレープ、慣れるとひょひょひょーいと書けてホントに便利です。

で、たまにあるんですよねちゃんとセキュリティしてらっしゃる会社さん。
実に素晴らしい事なのですが、レア過ぎてなかなか情報が無かったので突破法だけ書きたいと思います。

なぜできないか

  1. .pfx 証明書ファイルを自動で追加する方法がわからん - これが出る
    Screenshot 2019-11-29 at 19.12.32.png

  2. 追加できたんだけど、サイトにアクセスする度にブラウザがポップアップを開く(自動クリックめんどい)
    Screenshot 2019-11-29 at 19.14.01.png

対策は:

1. dockerfile内でユーザレベルで追加する
2. chrome policyで、証明書の自動セレクトを有効に

です。

dockerfile 作ろう

まずは dockerfile と同じ階層に下記のファイルを用意します。

/keys_dir.
key_password.txt - .pfxファイルのパスワードを平文で
key.pfx
policy.json - chrome policy 設定ファイル (下記)

Chrome policy ファイルの中身はこちら

policy.json
{
    "AutoSelectCertificateForUrls": ["{\"pattern\":\"*\",\"filter\":{}}"]
}

上記を用意したら、dockerfileを作成します。

FROM selenium/standalone-chrome-debug

# chrome policy
COPY keys/policies.json /etc/opt/chrome/policies/managed/
# key 関連
COPY keys/* /home/seluser/keys/

# 必要なツールをインストール
RUN sudo apt-get update
RUN sudo apt-get install libnss3-tools

# 証明書をユーザに追加
RUN mkdir -p /home/seluser/.pki/nssdb
RUN certutil -d /home/seluser/.pki/nssdb -N
RUN pk12util -d sql:/home/seluser/.pki/nssdb -i /home/seluser/keys/key.pfx -w /home/seluser/keys/key_password.txt

あとはお好きな名前をつけてビルドします

build.sh
docker build --rm --force-rm -t mytools/selenium-withcert .

実行しましょう。
僕はダウンロードに使うことが多いのでこのように -v でマウントします。

run.sh
docker run -d --restart always -v /dev/shm:/dev/shm -p 4444:4444 -p 5900:5900 -v ~/sel_dl:/home/seluser/Downloads --name selenium_cert mytools/selenium-withcert

以上です!!!
けっこう情報が無くて調べるの苦労しました。
いつか誰かの役に立つはず...

おまけ

たぶん関係ないと思うんですが、もし上でうまく行かなかった方のために、
参考までに他に僕がしてるprefs/argsの設定も晒しておきます。
(まさかのRだぞわっはっは。python/java他の方は読み替えをお願いします)

RSelenium_chromedriver.R
chrome_prefs = 
  list(  
    "profile.default_content_settings.popups" = 0L,
    "download.prompt_for_download" = FALSE,
    "download.directory_upgrade" = TRUE,
    "safebrowsing.enabled" = FALSE,
    "safebrowsing.disable_download_protection" = TRUE,
    "acceptSslCerts" = TRUE
  )

chrome_args = 
  c('--ignore-certificate-errors', 
    '--ignore-urlfetcher-cert-requests', 
    '--no-sandbox', 
    '--disable-gpu', 
    '--disable-web-security', 
    '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100')

eCaps_withhead = 
  list(chromeOptions = 
         list(
           prefs = chrome_prefs,
           args = chrome_args )
        )

# 初期化
remDr <- remoteDriver(
  remoteServerAddr = sel_ip,
  port = sel_port,
  browserName = "chrome",
  extraCapabilities = eCaps_withhead
)

[EOF]

1
1
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
1