Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
.ssh/configを管理する .ssh/configで管理する
Ken’ichiro Oyama
November 03, 2016
Technology
15
10k
.ssh/configを管理する .ssh/configで管理する
PHPカンファレンス2016
Ken’ichiro Oyama
November 03, 2016
Tweet
Share
More Decks by Ken’ichiro Oyama
See All by Ken’ichiro Oyama
k1low
0
180
k1low
1
190
k1low
5
1.3k
k1low
28
9.2k
k1low
0
99
k1low
0
390
k1low
1
1.9k
k1low
0
540
k1low
0
2.1k
Other Decks in Technology
See All in Technology
shimacos
2
360
ytaka23
4
860
terurou
0
140
layerx
1
960
kekeke_47
0
410
ryusa
2
320
yosshi_
2
180
takuros
3
590
nkjzm
1
860
hisaichi5518
0
170
fujiihda
8
1.1k
shirayanagiryuji
0
2.2k
Featured
See All Featured
samlambert
237
9.9k
paulrobertlloyd
71
3.6k
stephaniewalter
260
11k
pauljervisheath
196
15k
chriscoyier
499
130k
bermonpainter
342
26k
sferik
610
54k
jonrohan
1021
380k
reverentgeek
168
7.2k
jlugia
216
16k
chrislema
231
16k
jmmastey
9
540
Transcript
.ssh/configを管理する .ssh/configで管理する Kenʼichiro Oyama Fusic Co.,Ltd. 2016.11.3 1 PHPカンファレンス2016
みなさん SSHしてますか? 2 PHPカンファレンス2016
わたしは SSHしてます 3 PHPカンファレンス2016
?> NEXT PHPカンファレンス2016のテーマ 4 PHPカンファレンス2016
SSHとNEXT 5 PHPカンファレンス2016 FTP SSH + パスワード
SSH + 公開鍵認証 「SSHしたら負け」 <- たぶんここがNEXT Docker、EC2 Run Command、etc “NEXT”ではないですが、 今⽇は”SSH + 公開鍵認証”で少しだけ役に⽴ちそ うな話をします
Who 6 PHPカンファレンス2016
k1LoW Kenʼichiro Oyama @k1LoW Fusic Co.,Ltd. エンジニア
基盤ユニット テックリード GitHub organizations fukuokarb / dotcake / emacs-jp / etc. awspecというAWS⽤のテストツールを作って います https://github.com/k1LoW/awspec 7 PHPカンファレンス2016
普段のわたし 複数のプロジェクト 開発中 運⽤中 複数のサーバ
プロジェクト単位 ロール単位(Web、DB、etc) 本番、ステージング、テスト 8 PHPカンファレンス2016
イメージ 複数のプロジェクトの複数のサーバにSSH 9 PHPカンファレンス2016
~/.ssh/config SSH接続の管理をするための設定ファイル ~/.ssh/configはユーザごとの設定ファイル 10 PHPカンファレンス2016 Host github.com
User git Hostname github.com PreferredAuthentications publickey IdentityFile ~/.ssh/github_rsa
/home/k1low/.ssh/config 管理しているサーバへSSH接続するための設定 を記載 秘密鍵も .ssh/ 以下に配置 プロジェクトごと、ロールごと、サーバごとな
どで分けることが多い id_rsaは使わない 秘密鍵だけでなく、その他の接続設定も重要 HostnameやUser ProxyCommandとか 11 PHPカンファレンス2016
.ssh/config どうやって管理していますか? 12 PHPカンファレンス2016
.ssh/configの管理 configのバックアップ/リストア 秘密鍵のバックアップ/リストア サーバ接続情報の追加/削除 チームメンバーへの共有
踏み台サーバなど他サーバへの設置 13 PHPカンファレンス2016
configと秘密鍵に分かれていて ⾯倒 14 PHPカンファレンス2016
configは分割できないので メンバーへの共有も⾯倒 15 PHPカンファレンス2016
今のconfigとバックアップした configのマージも⾯倒 16 PHPカンファレンス2016
sconb 17 PHPカンファレンス2016
sconb “Ssh CONfig Buckup tool” .ssh/config専⽤のバックアップツール https://github.com/k1LoW/sconb
gem install sconb でインストール ※本⽇も⼤変お世話になっているCONBUとは無関係です 18 PHPカンファレンス2016
使い⽅ 19 PHPカンファレンス2016
バックアップ 20 PHPカンファレンス2016 $ sconb dump > ssh_config.json $ cat
ssh_config.json { "github.com": { "Host": "github.com", "User": "git", "Hostname": "github.com", "PreferredAuthentications": "publickey", "IdentityFile": [ "~/.ssh/github_rsa" ] }, ...
絞り込んでバックアップ 21 PHPカンファレンス2016 $ sconb dump example > ssh_config.json $ cat
ssh_config.json { ”example.com": { "Host": ”example.com", "User": ”k1low", "Hostname": ”dev.example.com", "PreferredAuthentications": "publickey", "IdentityFile": [ "~/.ssh/example_rsa" ] } }
リストア 22 PHPカンファレンス2016 $ sconb restore < ssh_config.json Host github.com
User git Hostname github.com PreferredAuthentications publickey IdentityFile ~/.ssh/github_rsa ... $ sconb restore < ssh_config.json > ~/.ssh/cofing
秘密鍵も含めて1ファイル化 23 PHPカンファレンス2016 $ sconb dump --all > ssh_config_with_key.json $
cat ssh_config_with_key.json { "github.com": { "Host": "github.com", "User": "git", "Hostname": "github.com", "PreferredAuthentications": "publickey", "IdentityFile": [ "~/.ssh/github_rsa" ], "IdentityFileContent": [ "-----BEGIN RSA PRIVATE KEY-----\nMIIEpXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX... ] }, ...
秘密鍵のリストア 24 PHPカンファレンス2016 $ sconb keyregen < ssh_config_with_key.json Regenerate ~/.ssh/github_rsa
... ...
つまり ssh_config <-> JSON のコンバータ 25 PHPカンファレンス2016
パイプで各種UNIXコマンドとも 連携できる 26 PHPカンファレンス2016
jqを使ったssh_confg.jsonのマージ jq: JSONをいい感じに操作できるコマンド 27 PHPカンファレンス2016 $ jq -s '.[0]
+ .[1]’ a.json b.json | sconb restore > ~/.ssh/config
.ssh/configを管理する sconbはおいしい 28 PHPカンファレンス2016
別の視点から 29 PHPカンファレンス2016
普段のわたし(再掲) 複数のプロジェクト 開発中 運⽤中 複数のサーバ
プロジェクト単位 ロール単位(Web、DB、etc) 本番、ステージング、テスト 30 PHPカンファレンス2016
イメージ(再掲) 複数のプロジェクトの複数のサーバにSSH 31 PHPカンファレンス2016
突然の◯penSSLの脆弱性発表 32 PHPカンファレンス2016
インベントリ情報 「サーバにイントールされているパッケージの バージョン⼀覧を再度確認して欲しい」 現在稼働しているサーバ数⼗台 まったく別の時期にまったく別のサービスを 構築している
全部SSHでログインして確認。。。??そして その後まとめるの。。。? ツラみがある。。。 33 PHPカンファレンス2016
34 koma PHPカンファレンス2016
koma エージェントレスでリモートホストのインベン トリ情報を収集するツール https://github.com/k1LoW/koma 類似ツール: ohai /
facter gem install koma でインストール `ssh` を `koma ssh` に変えるだけで、リモ ートホストのインベントリ情報をJSONで取得 できる 35 PHPカンファレンス2016
koma ssh 36 PHPカンファレンス2016 $ koma ssh example.com {
"memory": { "swap": { "cached": "652kB", "total": "2097148kB", "free": "2091376kB" }, "total": "1019956kB", "free": "255104kB", "buffers": "310644kB", "cached": "185748kB", ...
koma keys (収集できるインベントリ情報) memory ec2 (disabled) hostname
domain fqdn platform platform_version filesystem cpu virtualization kernel block_device package user group service PHPカンファレンス2016 37
koma run-command 38 PHPカンファレンス2016 $ koma run-command example.com,example.jp uptime {
"example.com": { "uptime": { "exit_signal": null, "exit_status": 0, "stderr": "", "stdout": " 00:18:10 up 337 days, 4:51, 1 user, load average: 0.08, 0.02, 0.01\n" } }, "example.jp": { "uptime": { ...
そう、.ssh/configが設定されて いればね! 39 PHPカンファレンス2016
.ssh/configで管理する 40 PHPカンファレンス2016
ssh_config形式で管理する .ssh/configで管理する 41 PHPカンファレンス2016
koma with ssh_config komaはssh_config形式のフォーマットを STDINから読み込んで実⾏することができる Vagrant上のOSのcpu情報を取得 sconbをでHostをフィルタリング
42 PHPカンファレンス2016 $ vagrant ssh-config | koma ssh --key cpu $ sconb dump dev.* | sconb restore | koma ssh
Vuls 43 PHPカンファレンス2016
Vuls “VULnerability Scanner” バルス Linux/FreeBSD向けの脆弱性スキャンツール Ubuntu、Debian、CentOS、Amazon
Linux、RHELに対応 標準のTUIだけでなくVulsRepoというWebビューワもある NVDやJVNの脆弱性情報などを使⽤してスキャン スキャン対象のサーバーへは “SSH” で接続 TOMLで設定を記述 44 PHPカンファレンス2016
SSH。。。だと。。? 45 PHPカンファレンス2016
ssh_config_to_vuls_config 46 PHPカンファレンス2016
ssh_config_to_vuls_config ssh_config形式をVuls⽤のTOMLファイル形式 に変換 sc2vcコマンド https://github.com/k1LoW/ssh_config_to_vuls_config gem
install ssh_config_to_vuls_config でインストール (当然)sconbでフィルタリングも可能 47 PHPカンファレンス2016 $ cat ~/.ssh/config | sc2vc > vuls_config.toml $ sconb dump dev.* | sconb restore | sc2vc > filtered_config.toml
.ssh/config -> JSON JSON -> .ssh/config JSON -> koma JSON
-> Vuls 48 PHPカンファレンス2016
NEXT 49 PHPカンファレンス2016
ssh_configファイルを集約すれば サーバインベントリ情報も 脆弱性情報も 管理できるのでは? 50 PHPカンファレンス2016
こんなサーバを 51 PHPカンファレンス2016
.ssh/configで管理 52 PHPカンファレンス2016
komad (仮) sconbなJSONをAPI or SCP経由で登録 ssh_config + komaでサーバインベントリ情報
を収集管理 ssh_config + Vulsで脆弱性情報を収集管理 koma run-commandでSSHを通じた外形監視 ちょっと変わったサーバ管理/監視システムが できそう 53 PHPカンファレンス2016
.ssh/configで管理する 54 PHPカンファレンス2016
まとめ 55 PHPカンファレンス2016
まとめ .ssh/configをsconbでJSON形式にすることで 管理容易性、再利⽤性を向上 .ssh/configやssh_config形式のフォーマット を⼊⼒として動くエージェントレスなサーバイ ンベントリ収集ツール koma
Vulsの設定の元として.ssh/configの活⽤ ssh_configを活⽤したサーバ管理/監視システ ムkomad(仮)の可能性 56 PHPカンファレンス2016
さっそく .ssh/configを整理しましょう 古い設定、残っていませんか? 57 PHPカンファレンス2016
Thank you! Fusicはテクノロジーが 好きなエンジニアを募集しています https://fusic.github.io 58
?> 59 PHPカンファレンス2016
NEXT 60 PHPカンファレンス2016
イメージ(再掲) 複数のプロジェクトの複数のサーバにSSH 61 PHPカンファレンス2016
サーバのユーザを管理したい どのサーバにどのユーザがSSHログインできる のか 簡単なものでいい ある程度でいい もしくはSSHログインできる情報をもらえるよ
うにしたい 公開鍵とか秘密鍵とかの管理 62 PHPカンファレンス2016
⼀般的な⽅法 63 PHPカンファレンス2016
PAM Pluggable Authentication Module PAMを使ってユーザ管理サーバ(認証基盤)に 認証の要求をもらう ADとかLDAPが⼀般的らしい
ただ個⼈的にはSTNS推し シンプル 設定がTOML 認証サーバ側をサーバレスにできそう http://qiita.com/shogomuranushi/items/ f09fcdeb146b45452403 新イケメンことP⼭さん(@pyama86)が開発している 64 PHPカンファレンス2016
だかしかし 65 PHPカンファレンス2016
プロジェクトを横断できない 完全弊社都合 66 PHPカンファレンス2016
komad(仮)がssh_configを 払い出したらどうだろう 67 PHPカンファレンス2016
komad(仮)にもらう API経由でssh_config.jsonをもらう 68 PHPカンファレンス2016
どうでしょう。。? イビツなのは承知 komad(仮)サーバの運⽤。。。 無理があるか。。。 69 PHPカンファレンス2016
Thank you! Fusicはテクノロジーが 好きなエンジニアを募集しています https://fusic.github.io 70