<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>go | pyama.fun</title>
	<atom:link href="https://pyama.fun/archives/category/go/feed" rel="self" type="application/rss+xml" />
	<link>https://pyama.fun</link>
	<description>Our life is our art.</description>
	<lastBuildDate>Fri, 06 Nov 2020 01:50:12 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
<site xmlns="com-wordpress:feed-additions:1">140258140</site>	<item>
		<title>Linuxのユーザー管理基盤ミドルウェアのSTNSで公開鍵認証を実装できるライブラリを公開しました</title>
		<link>https://pyama.fun/archives/6022</link>
		
		<dc:creator><![CDATA[pyama]]></dc:creator>
		<pubDate>Fri, 06 Nov 2020 01:50:12 +0000</pubDate>
				<category><![CDATA[go]]></category>
		<category><![CDATA[STNS]]></category>
		<guid isPermaLink="false">https://ten-snapon.com/?p=2539</guid>

					<description><![CDATA[<p>STNSとは ざっくりSTNSを利用するとこれまでLDAPなどで管理されてきたLinuxのユーザーをTOMLフォーマットの設定ファイルやDynamoDB、S3で管理できるようになります。またSSHログインの公開鍵認証など &#8230; </p>
<p class="link-more"><a href="https://pyama.fun/archives/6022" class="more-link">続きを読む<span class="screen-reader-text"> "Linuxのユーザー管理基盤ミドルウェアのSTNSで公開鍵認証を実装できるライブラリを公開しました"</span></a></p>
The post <a href="https://pyama.fun/archives/6022">Linuxのユーザー管理基盤ミドルウェアのSTNSで公開鍵認証を実装できるライブラリを公開しました</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">STNSとは</h2>



<script async class="speakerdeck-embed" data-id="ab87c720e8544d06a55d452fda1c6bbe" data-ratio="1.33333333333333" src="//speakerdeck.com/assets/embed.js"></script>



<p>ざっくりSTNSを利用するとこれまでLDAPなどで管理されてきたLinuxのユーザーをTOMLフォーマットの設定ファイルやDynamoDB、S3で管理できるようになります。またSSHログインの公開鍵認証などをかんたんに実現できるのがSTNSの優位性でした。</p>



<h2 class="wp-block-heading">libstns-go</h2>



<p>今回そのSTNSを利用して、下記のライブラリを活用するとあらゆるWEBサービスやCLIツールで公開鍵認証が可能になります。</p>


<div class="linkcard"><div class="lkc-external-wrap"><a class="lkc-link no_icon" href="https://github.com/STNS/libstns-go" data-lkc-id="1275" target="_blank" rel="external noopener"><div class="lkc-card"><div class="lkc-info"><div class="lkc-favicon"><img decoding="async" src="https://www.google.com/s2/favicons?domain=github.com" alt="" width="16" height="16" /></div><div class="lkc-domain">GitHub</div></div><div class="lkc-content"><figure class="lkc-thumbnail"><img decoding="async" class="lkc-thumbnail-img" src="//pyama.fun/wp-content/uploads/pz-linkcard/cache/e6764ae62f4345e9b71f3069352b4451c393f29df65b85c3c5324de4e8dffa9b.jpeg" width="100px" height="108px" alt="" /></figure><div class="lkc-title">GitHub - STNS/libstns-go: STNS client library for Go</div><div class="lkc-url" title="https://github.com/STNS/libstns-go">https://github.com/STNS/libstns-go</div><div class="lkc-excerpt">STNS client library for Go. Contribute to STNS/libstns-go development by creating an account on GitHub.</div></div><div class="clear"></div></div></a></div></div>



<p>サンプルコードとしては下記のようなものになります。</p>



<pre class="wp-block-code"><code>package main

import (
	"fmt"

	"github.com/STNS/libstns-go/libstns"
	"github.com/k0kubun/pp"
)

func main() {
	stns, err := libstns.NewSTNS("https://stns.lolipop.io/v1/", nil)
	if err != nil {
		panic(err)
	}

	user, err := stns.GetUserByName("pyama")
	if err != nil {
		panic(err)
	}
	pp.Println(user)
        // id_rsaキーで署名する
	signature, err := stns.Sign(&#091;]byte("secret test"))
	if err != nil {
		panic(err)
	}

	// pyamaの公開鍵で署名検証できた
	fmt.Println(stns.VerifyWithUser("pyama", &#091;]byte("secret test"), signature))

	// 文字列が異なるので署名検証できない
	fmt.Println(stns.VerifyWithUser("pyama", &#091;]byte("make error"), signature))

}</code></pre>



<p>実装としては任意の値を <code>~/.ssh/id_rsa</code> などの秘密鍵で署名し、その署名をSTNSで公開されている公開鍵で検証し、検証成功すればそれはSTNSに登録されたユーザーであると認証することができます。</p>



<figure class="wp-block-image size-large"><img decoding="async" data-attachment-id="2540" data-permalink="https://pyama.fun/archives/2541/9527e66d-cdd5-4ac5-bcfb-6bbf38ca915a-435-0000001487f13c6a_file" data-orig-file="https://pyama.fun/wp-content/uploads/2019/01/9527e66d-cdd5-4ac5-bcfb-6bbf38ca915a-435-0000001487f13c6a_file.jpg" data-orig-size="810,1080" data-comments-opened="0" data-image-meta="{&quot;aperture&quot;:&quot;1.8&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone X&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1547513895&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4&quot;,&quot;iso&quot;:&quot;160&quot;,&quot;shutter_speed&quot;:&quot;0.25&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}" data-image-title="9527e66d-cdd5-4ac5-bcfb-6bbf38ca915a-435-0000001487f13c6a_file" data-image-description="" data-image-caption="" data-large-file="https://pyama.fun/wp-content/uploads/2019/01/9527e66d-cdd5-4ac5-bcfb-6bbf38ca915a-435-0000001487f13c6a_file-768x1024.jpg" src="https://pyama.fun/wp-content/uploads/2020/11/image-1024x385.png" alt="" class="wp-image-2540"/></figure>



<p>勘の良い方は気づいたかと思いますが、SSHのログイン時に利用されている公開鍵認証相当のことをあらゆるシステムで可能にしたというのが今回の実装です。</p>



<h2 class="wp-block-heading">ユースケース</h2>



<p>まず公開鍵認証を利用することで一般的なID/Password認証よりも比較的暗号強度が高い認証を行うことができます。さらには多要素認証の一つとして活用いただくのも良いと思います。</p>



<p>あとは社内で利用するツールを開発するときに、OAuth認証は容易に利用できるので僕も重宝しているのですが、CLIツールから利用するとなると非常に手間がかかります。そういった場合に、この仕組を利用するとCLIで完結するかつ、強力な認証を利用できます。</p>



<p></p>



<h2 class="wp-block-heading">最後に</h2>



<p>今回実装してみて思ったのですが世の中意外とそんなに公開鍵認証って実装されてるものって少ないなぁと思ったりもしました。今回の実装の追加でSTNSを利用したおもしろ隙間ソフトウェアとかもできるようになると思うので、是非活用いただいて、ブログなど書いていただけると大変に嬉しく思います。</p>



<p>ちょっとかんたんに試してみたいのだけど！という方がいらっしゃいましたら、STNS as a Serviceもあるのでお試しください。</p>


<div class="linkcard"><div class="lkc-external-wrap"><a class="lkc-link no_icon" href="https://github.com/STNS/as_a_service" data-lkc-id="1276" target="_blank" rel="external noopener"><div class="lkc-card"><div class="lkc-info"><div class="lkc-favicon"><img decoding="async" src="https://www.google.com/s2/favicons?domain=github.com" alt="" width="16" height="16" /></div><div class="lkc-domain">GitHub</div></div><div class="lkc-content"><figure class="lkc-thumbnail"><img decoding="async" class="lkc-thumbnail-img" src="https://s.wordpress.com/mshots/v1/https%3A%2F%2Fgithub.com%2FSTNS%2Fas_a_service?w=200" width="100px" height="108px" alt="" /></figure><div class="lkc-title">GitHub - STNS/as_a_service: STNS as a Service</div><div class="lkc-url" title="https://github.com/STNS/as_a_service">https://github.com/STNS/as_a_service</div><div class="lkc-excerpt">STNS as a Service. Contribute to STNS/as_a_service development by creating an account on GitHub.</div></div><div class="clear"></div></div></a></div></div>The post <a href="https://pyama.fun/archives/6022">Linuxのユーザー管理基盤ミドルウェアのSTNSで公開鍵認証を実装できるライブラリを公開しました</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">6022</post-id>	</item>
		<item>
		<title>VaultからTLS認証に利用する情報を安全に取得するKagianaというソフトウェアを書いた</title>
		<link>https://pyama.fun/archives/6021</link>
		
		<dc:creator><![CDATA[pyama]]></dc:creator>
		<pubDate>Mon, 19 Oct 2020 02:27:06 +0000</pubDate>
				<category><![CDATA[go]]></category>
		<guid isPermaLink="false">https://ten-snapon.com/?p=2507</guid>

					<description><![CDATA[<p>ペパボでは新型コロナウィルスをきっかけに、全社でのリモートワークを前提とした勤務体制を取っています。それにあたり、いろいろな工夫をしているわけですが、開発者が利用するイントラAPIなどを安全に利用するために、VPNを利用 &#8230; </p>
<p class="link-more"><a href="https://pyama.fun/archives/6021" class="more-link">続きを読む<span class="screen-reader-text"> "VaultからTLS認証に利用する情報を安全に取得するKagianaというソフトウェアを書いた"</span></a></p>
The post <a href="https://pyama.fun/archives/6021">VaultからTLS認証に利用する情報を安全に取得するKagianaというソフトウェアを書いた</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></description>
										<content:encoded><![CDATA[<p>ペパボでは新型コロナウィルスをきっかけに、全社でのリモートワークを前提とした勤務体制を取っています。それにあたり、いろいろな工夫をしているわけですが、開発者が利用するイントラAPIなどを安全に利用するために、VPNを利用するケースは非常に多いと思います。しかし、VPNについてはネットワークによる認可の細かい制御が難しく、基本的にVPNのエンドポイントにつなげると何でもできてしまうという状態になってしまいがちです。そういった課題を解決するために、僕が社内に提供するサービスはなるべくTLSクライアント認証を利用して、ネットワークの認証を行ったあとに、更にアプリケーションで認証・認可を行うようにしています。</p>



<p>またTLSクライアント認証に利用するクライアント証明書、鍵のセットはHashicorp VaultのPKI基盤を利用して払い出しており、こちらもGHEのOrganization/Repository/Teamの情報とリンクする形式で認可を行っています。</p>



<p>一方でHashicorp VaultはAPIを実行したり、クライアントCLIを利用したり、または管理者向けのWEBUIはあれど、この手のミドルウェアを利用したことがないエンジニアに、ここから認証後に鍵取得して、あとはよろーというには少々厳しい現実があります。</p>



<p>それらを解決するために開発したのがKagianaです。</p>


<div class="linkcard"><div class="lkc-external-wrap"><a class="lkc-link no_icon" href="https://github.com/pyama86/kagiana" data-lkc-id="1273" target="_blank" rel="external noopener"><div class="lkc-card"><div class="lkc-info"><div class="lkc-favicon"><img decoding="async" src="https://www.google.com/s2/favicons?domain=github.com" alt="" width="16" height="16" /></div><div class="lkc-domain">GitHub</div></div><div class="lkc-content"><figure class="lkc-thumbnail"><img decoding="async" class="lkc-thumbnail-img" src="//pyama.fun/wp-content/uploads/pz-linkcard/cache/f93346cfa03ccd23b678495cbfe03cac100786def99db24b026b667365fb75d5.jpeg" width="100px" height="108px" alt="" /></figure><div class="lkc-title">GitHub - pyama86/kagiana: It is OAuth Authenticator &amp; Vault Certificate Getter</div><div class="lkc-url" title="https://github.com/pyama86/kagiana">https://github.com/pyama86/kagiana</div><div class="lkc-excerpt">It is OAuth Authenticator &amp; Vault Certificate Getter - pyama86/kagiana</div></div><div class="clear"></div></div></a></div></div>



<p>Kagiana自体はGitHub(厳密にはGitHub Enterpriseを利用しています) の提供するOAuth認証を行ったあとに、Kagianaで指定したPKIエンドポイントから証明書と鍵のダウンロードを行います。イメージとしては下記のとおりです。要するにKagianaの設定ファイルで指定してある証明書、鍵をWEBUIで認証し、払い出すことができる仕組みです。</p>



<figure class="wp-block-image size-large"><img decoding="async" data-attachment-id="2510" data-permalink="https://pyama.fun/archives/6021/0d40a5e4a645fc6b96e767d64ac0878e" data-orig-file="https://pyama.fun/wp-content/uploads/2020/10/0d40a5e4a645fc6b96e767d64ac0878e.png" data-orig-size="1776,1019" data-comments-opened="0" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="0d40a5e4a645fc6b96e767d64ac0878e" data-image-description="" data-image-caption="" data-large-file="https://pyama.fun/wp-content/uploads/2020/10/0d40a5e4a645fc6b96e767d64ac0878e.png" src="https://pyama.fun/wp-content/uploads/2020/10/0d40a5e4a645fc6b96e767d64ac0878e-1024x588.png" alt="" class="wp-image-2510"/></figure>



<p>ペパボではKagianaで認証後に、Vaultに接続が可能な証明書、鍵をダウンロードすることで、利用者をVaultに接続可能にします。その後、Vaultで認証・認可を行うことで、そのユーザーが利用可能な証明書と鍵がダウンロードできるようになります。</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://pyama.fun/wp-content/uploads/2020/10/image-1024x515.png" alt="" class="wp-image-2513"/></figure>



<p>更に、上記図の5にあたる証明書の管理については <code>consul-template</code> を手元の端末で起動できるスクリプトをLinux、MacOSの環境それぞれ準備しており、インターナルなKagianaの管理リポジトリで、 <code>make install</code> と実行するだけで、利用者の手元で <code>consul-template</code> が起動し、自動で証明書の取得、更新管理を行ってくれます。配布している <code>consul-temlate</code> の設定は下記のようなものです。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
max_stale = &quot;10m&quot;
log_level = &quot;info&quot;
pid_file = &quot;/tmp/consul-template.pid&quot;

vault {
  address = &quot;https://vault.example.com&quot;
  renew_token = true

  ssl {
    enabled = true
    verify = true
    ca_path = &quot;~/.kagiana/vault.example.com.ca&quot;
    cert = &quot;~/.kagiana/vault.example.com.cert&quot;
    key = &quot;~/.kagiana/vault.example.com.key&quot;
  }
}

template {
contents    = &quot;{{ with secret &quot;example.com/issue/example.com&quot; &quot;common_name=vault.example.com&quot; }}{{ .Data.issuing_ca }}{{ end }}&quot;
  destination = &quot;~/.kagiana/vault.example.com.ca&quot;
}

template {
  contents    = &quot;{{ with secret &quot;example.com/issue/example.com&quot; &quot;common_name=vault.example.com&quot; }}{{ .Data.certificate }}{{ end }}&quot;
  destination = &quot;~/.kagiana/vault.example.com.cert&quot;
}

template {
  contents    = &quot;{{ with secret &quot;example.com/issue/example.com&quot; &quot;common_name=vault.example.com&quot; }}{{ .Data.private_key }}{{ end }}&quot;
  destination = &quot;~/.kagiana/vault.example.com.key&quot;
}

</pre></div>


<p>このような設定ファイルを定義して起動すると、<strong>Vaultから取得した証明書の有効期限を <code>consul-template</code> が監視し、自動で証明書を失効前に更新してくれます</strong>。さらには必要に応じて、更新したタイミングで何かしらのコマンドを実行することができます。例えばWEBサーバであれば証明書を更新したあとにWEBサーバをリロードするというような定義をすることはよくあります。</p>



<p>ちなみに、なぜ <code>consul-template</code> を利用するのかというと、<strong>これらの仕組みで提供する証明書の有効期限を1日程度と非常に短い値にすることで、主に退職時などにおける鍵の失効管理をなくしたいという狙い</strong>があるためです。実態としてはVaultでアカウントが認証できなくなることで鍵の更新ができなくなり、自動的に失効するような運用を行っています。一方であまりに短い期間で証明書を手動で更新するのは非常に手間なので、 <code>consul-template</code> で更新を自動化しているということです。更にこの仕組を利用すればリポジトリにおいてある、 <code>consul-template</code> の設定ファイルを更新、配布するだけで全開発者に利用を許可する証明書を一括で配布することができるので管理者としても非常に運用が楽です。</p>



<p>ここで一つ与太話なのですが、Linux、MacOSの各環境の <code>consul-template</code> の起動スクリプト生成に <code>Foreman</code> を利用しています。僕も今回知ったのですが、 <code>Foreman</code> には <code>export</code> というサブコマンドがあり、下記のようなProcfileをまず定義します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
consul-template: /usr/bin/env PATH=$PATH:/usr/local/bin consul-template -config=./consul-template/my-config.hcl
</pre></div>


<p>そして <code>foreman export systemd &lt;dest&gt; &lt;option&gt; -f myProcfile</code> と実行すると<code>systemd</code> の起動スクリプトを出力してくれます。そしてこれが <code>launchd</code> などにも対応しているので、各環境の起動スクリプトのグルー技術として非常に便利でした。下記のリンクにある通り、いろいろな出力が可能なので知っておくと、今後Linuxデスクトップも増えてくると思うので便利だと思います。</p>


<div class="linkcard"><div class="lkc-external-wrap"><a class="lkc-link no_icon" href="https://github.com/ddollar/foreman/tree/master/data/export" data-lkc-id="1274" target="_blank" rel="external noopener"><div class="lkc-card"><div class="lkc-info"><div class="lkc-favicon"><img decoding="async" src="https://www.google.com/s2/favicons?domain=github.com" alt="" width="16" height="16" /></div><div class="lkc-domain">GitHub</div></div><div class="lkc-content"><figure class="lkc-thumbnail"><img decoding="async" class="lkc-thumbnail-img" src="//pyama.fun/wp-content/uploads/pz-linkcard/cache/1d4382d5cf0ac0ae8feea4172f4f2e657a9c26582ed2556c44b48461da68fcc3.jpeg" width="100px" height="108px" alt="" /></figure><div class="lkc-title">foreman/data/export at main · ddollar/foreman</div><div class="lkc-url" title="https://github.com/ddollar/foreman/tree/master/data/export">https://github.com/ddollar/foreman/tree/master/data/export</div><div class="lkc-excerpt">Manage Procfile-based applications. Contribute to ddollar/foreman development by creating an account on GitHub.</div></div><div class="clear"></div></div></a></div></div>



<h2 class="wp-block-heading">最後に</h2>



<p>今回はTLSクライアント認証に利用する証明書、鍵を何かしらの認証後に取得し、その証明書を利用して必要な認証、認可をしていくというようなまさに鍵穴となるようなソフトウェアを開発しました。おそらくこの仕組はしばらくペパボ内でレバレッジを効かせながら使われていくソフトウェアになると思うので継続開発していく予定です。同じような課題をお持ちの方、認証方法の追加のご希望などあればIssueを書いていただければ諸々検討するので、ぜひお気軽にご連絡ください。</p>The post <a href="https://pyama.fun/archives/6021">VaultからTLS認証に利用する情報を安全に取得するKagianaというソフトウェアを書いた</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">6021</post-id>	</item>
		<item>
		<title>k8sでクラスタの誤操作を防ぐラッパーをリリースしました(Check the cluster with kubectl)</title>
		<link>https://pyama.fun/archives/5998</link>
		
		<dc:creator><![CDATA[pyama]]></dc:creator>
		<pubDate>Wed, 05 Jun 2019 03:09:15 +0000</pubDate>
				<category><![CDATA[go]]></category>
		<guid isPermaLink="false">https://ten-snapon.com/?p=2231</guid>

					<description><![CDATA[<p>解決したい課題 僕は現在GMOペパボの技術基盤チームに所属しており、ペパボの商材であるロリポップやColormeなどで動作する基盤の開発に携わっています。昨今はもっぱらk8sを触っているのですが、その際に起こりうるオペミ &#8230; </p>
<p class="link-more"><a href="https://pyama.fun/archives/5998" class="more-link">続きを読む<span class="screen-reader-text"> "k8sでクラスタの誤操作を防ぐラッパーをリリースしました(Check the cluster with kubectl)"</span></a></p>
The post <a href="https://pyama.fun/archives/5998">k8sでクラスタの誤操作を防ぐラッパーをリリースしました(Check the cluster with kubectl)</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></description>
										<content:encoded><![CDATA[<iframe class="hatenablogcard" style="width:100%;height:155px;margin:15px 0;max-width:680px;" title="pyama86/cluster-caution: Prevents execution errors in kubectl" src="https://hatenablog-parts.com/embed?url=https://github.com/pyama86/cluster-caution" frameborder="0" scrolling="no"></iframe>



<h2 class="wp-block-heading">解決したい課題</h2>



<p>僕は現在GMOペパボの技術基盤チームに所属しており、ペパボの商材であるロリポップやColormeなどで動作する基盤の開発に携わっています。昨今はもっぱらk8sを触っているのですが、その際に起こりうるオペミスは意図していないClusterやNamespaceへの操作が考えられます。下記のようなオペミスをして、意図しないNamespaceのポッドを全消ししたなんて経験、<strong>皆さんありますよね？？？？</strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
$ kubectl delete pod --all   
</pre></div>


<p>これを解決するために、<code>kubectl</code> コマンドのラッパー(1.12移行ではプラグインとしても機能)を開発しました。</p>



<h2 class="wp-block-heading">使い方</h2>



<p>僕は下記のように利用しています。</p>



<p>まずコマンドが長いのでaliasに登録します。これは実際には <code>.zshrc</code> に書いています。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
$ alias kc=&#039;kubectl-cluster-caution&#039;
</pre></div>


<p>次に、作業するリポジトリで下記を実行してください。これを実行すると、現在のk8sコンテキスト、Namespaceをリポジトリトップの <code>.kubectl-cluster-caution</code>　ファイルに出力します。このファイルを元に、現在のコンテキストを突合します。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
$ kc --add-context
</pre></div>


<p>この操作のあとに例えばクラスタを変更して、k8sコマンドを実行してみます。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
$ kc config use-context other.example.com
Switched to context &quot;other.example.com&quot;.
$ kc get pod
Repository configuration is different from cluster or namespace.
Do you want to continue?(Y/n) (yes/no) &#x5B;yes]: 
</pre></div>


<p>このように定義された情報と異なる場合は、<code>yes/no</code>の確認が入るので、一回気づくチャンスが増えます。</p>



<h2 class="wp-block-heading">導入方法</h2>



<p>brewを提供しています。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
$ brew tap pyama86/ptools
$ brew install cluster-caution
</pre></div>


<h2 class="wp-block-heading">最後に</h2>



<p>今後、k8sの導入が進んでくると、僕のような働き方じゃなくても、起こり得るオペミスだと思うのでぜひ導入していただき、皆さんの運用に貢献できたら大変嬉しいです。</p>The post <a href="https://pyama.fun/archives/5998">k8sでクラスタの誤操作を防ぐラッパーをリリースしました(Check the cluster with kubectl)</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5998</post-id>	</item>
		<item>
		<title>Apache httpdのコンフィグをパースしてJSONにする</title>
		<link>https://pyama.fun/archives/5988</link>
		
		<dc:creator><![CDATA[pyama]]></dc:creator>
		<pubDate>Thu, 15 Nov 2018 01:31:39 +0000</pubDate>
				<category><![CDATA[go]]></category>
		<guid isPermaLink="false">https://ten-snapon.com/?p=2081</guid>

					<description><![CDATA[<p>昨夜書くぞーという気持ちが急に生まれて、久しぶりに6時間くらいぶっ通しで書いた。 解決したかった課題 筆者に所属するGMOペパボでは数百万サイトをホスティングしており、それらの配信はhttpdにより行われています。htt &#8230; </p>
<p class="link-more"><a href="https://pyama.fun/archives/5988" class="more-link">続きを読む<span class="screen-reader-text"> "Apache httpdのコンフィグをパースしてJSONにする"</span></a></p>
The post <a href="https://pyama.fun/archives/5988">Apache httpdのコンフィグをパースしてJSONにする</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></description>
										<content:encoded><![CDATA[<p>昨夜書くぞーという気持ちが急に生まれて、久しぶりに6時間くらいぶっ通しで書いた。</p>
<p><iframe style="border:none;display:block;margin:0 0 1.7rem;overflow:hidden;height:155px;width:100%;max-width:100%;" title="pyama86/panalysis: Analyze the configuration of httpd and make it json" src="//hatenablog-parts.com/embed?url=https://github.com/pyama86/panalysis"></iframe></p>
<h1>解決したかった課題</h1>
<p>筆者に所属するGMOペパボでは数百万サイトをホスティングしており、それらの配信はhttpdにより行われています。httpdの設定は設定をAPI経由で配布されるデーモンによって行われておりますが、古いアーキテクチャに起因し、一部の情報が配布した設定ファイルにしかないという課題がありました。例えばある設定をAPI経由でサーバに設定したが、どの値が設定されているかはDBなどに情報はなく、設定ファイルをみるしかないという感じです。</p>
<p>そういった状況だと普段運用している場合にはほとんど困ることがないのですが、例えばスケジューラーによって動的に配信ホストを変えたりだとか、FUSEを利用して設定ファイルをプラガブルに配信しようと思った場合に、設定ファイルの取り回しがどうしてもネックになってしまいます。</p>
<p>FUSEについては同僚のインフラ・ジーニアス @harasouの書いたブログがロックです。</p>
<p><iframe style="border:none;display:block;margin:0 0 1.7rem;overflow:hidden;height:155px;width:100%;max-width:100%;" title="libfuse で Hello World !" src="//hatenablog-parts.com/embed?url=https://harasou.jp/2017/12/04/fuse/"></iframe></p>
<h1>Panalysisが提供すること</h1>
<p>Panalysisではhttpdのコンフィグを読み込み、JSONに構造化することと、JSONからhttpdのコンフィグを生成することをサポートしています。なぜ双方向をサポートしているかというと、Panalysis自体の動作正常性を見る場合に、既存のコンフィグとJSONの一致化検証をするに当たり、差分を見たかったからです。</p>
<blockquote><p>
  要はこういうことがやりたい</p>
<pre class="brush: bash; title: ; notranslate">
# configからjson作る
$ cat misc/sample.conf | pkg/darwin_amd64/panalysis -c | jq &gt; /tmp/conf_to_json;
# jsonからconfig作る
$ cat /tmp/conf_to_json | pkg/darwin_amd64/panalysis -j &gt; /tmp/json_to_conf;
# コメントと空白を無視してDIFFとる
$ diff -I '^\s*\#' -Bw misc/sample.conf /tmp/json_to_conf;
194,195d67
&lt;       #PHPIniDir &quot;C:/Windows&quot;
&lt;
</pre>
</blockquote>
<h1>Panalysisを使って今後、何をやるか</h1>
<p>まずは既存のhttpdの設定ファイルをすべてDBに吸い上げ、FUSEで管理できるようにします。その後は、アーキテクチャを組み換え、動的な配信サーバのスケジューリングや、 <strong>止まらない超高集積WEBサーバ</strong>を実現していきたいと思っています。僕と一緒に要素技術を生み出しながら世の中にないアーキテクチャを作ること、コードの力で運用を変えつつ、その先の世界を変える仕事を一緒にやりたい仲間を募集しています。</p>The post <a href="https://pyama.fun/archives/5988">Apache httpdのコンフィグをパースしてJSONにする</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5988</post-id>	</item>
		<item>
		<title>Linuxユーザーと公開鍵を統合管理するサーバ&#038;クライアントのSTNSを書き直した</title>
		<link>https://pyama.fun/archives/5985</link>
		
		<dc:creator><![CDATA[pyama]]></dc:creator>
		<pubDate>Mon, 10 Sep 2018 03:35:52 +0000</pubDate>
				<category><![CDATA[go]]></category>
		<category><![CDATA[STNS]]></category>
		<guid isPermaLink="false">https://ten-snapon.com/?p=2017</guid>

					<description><![CDATA[<p>この度、Linuxユーザーと公開鍵を統合管理するサーバ&#38;クライアントのSTNSをすべて書き直し、v2をリリースしたので、お知らせします。 v1リリース時の記事は下記にあります。 はじめに STNS v2のリリース &#8230; </p>
<p class="link-more"><a href="https://pyama.fun/archives/5985" class="more-link">続きを読む<span class="screen-reader-text"> "Linuxユーザーと公開鍵を統合管理するサーバ&#038;クライアントのSTNSを書き直した"</span></a></p>
The post <a href="https://pyama.fun/archives/5985">Linuxユーザーと公開鍵を統合管理するサーバ&クライアントのSTNSを書き直した</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></description>
										<content:encoded><![CDATA[<p>この度、Linuxユーザーと公開鍵を統合管理するサーバ&amp;クライアントのSTNSをすべて書き直し、v2をリリースしたので、お知らせします。</p>
<p>v1リリース時の記事は下記にあります。</p>
<p><iframe class="hatenablogcard" style="width:100%;height:155px;margin:15px 0;max-width:680px;" title="Linuxユーザーと公開鍵を統合管理するサーバ&#038;クライアントを書いた[更新] | ten-snapon.com" src="https://hatenablog-parts.com/embed?url=https://ten-snapon.com/archives/1228" frameborder="0" scrolling="no"></iframe></p>
<h1>はじめに</h1>
<p>STNS v2のリリースに当たり、改めてLinuxの名前解決について紹介します。<br />
Linuxはファイルのパーミッションやログイン権限の管理にユーザー名やグループ名が使われています。しかし、実際にパーミッションに利用されている値はID値であり、それらを名前と紐付けるためにいわゆる名前解決が必要です。</p>
<pre class="brush: bash; title: ; notranslate">
$ id example
uid=2000(example) gid=2000(example) groups=2000(example)
</pre>
<p>この例ですと、<code>example</code>ユーザーは<code>ID 2000</code>を持つユーザーであることがわかります。</p>
<p>これらの名前解決を行うにあたり、Linux標準の仕組みですと<code>useradd</code>コマンドでユーザーを追加すると、通常は<code>/etc/passwd</code>や<code>/etc/group</code>、<code>/etc/shadow</code>ファイルにユーザーやグループの情報が書き込まれるのですが、昨今、デザイナーによるデプロイのためのログインなど多くの職種がサーバにアクセスする機会が増えており、それらを手で管理したり、プロビジョニングツールで管理するのはあまりベターな仕組みとは言えません。また人材の流動性も上がっており、入退職もしやすくなったため、ワンストップでユーザー管理ができる必要があります。</p>
<p>このようなユーザー管理を行うにあたり、ある程度サービス規模が大きくなってくるとLDAPやDBを利用してユーザー管理を行うのが常であったと思いますが、LDAPやDBでの管理はWeb業界で好まれるコード管理やGitHub WorkFlowと必ずしも相性がよくありません。</p>
<p>それらを解決するために、筆者が開発したのが<strong>Simple Toml Name Service</strong>です。</p>
<p>v2のアーキテクチャはこちらの図のとおりです。</p>
<p><img fetchpriority="high" decoding="async" src="https://stns.jp/images/stns-architecture.png" width="1071" height="491" class="alignnone size-large" /></p>
<p>STNSを利用すると下記のようなシンプルな設定ファイルでユーザー・グループの統合管理を行うことができます。</p>
<pre class="brush: bash; title: ; notranslate">

&#x5B;users.pyama]
id = 1001
group_id = 1001
directory = &quot;/home/pyama&quot;
shell = &quot;/bin/bash&quot;
keys = &#x5B;&quot;ssh xxxxxxx&quot;]

&#x5B;groups.pyama]
id = 1001
users = &#x5B;&quot;pyama&quot;]

</pre>
<p>筆者の所属するGMOペパボでは、この設定ファイルをThorスクリプトを利用して、GitHubAPIから自動で生成し、ユーザー・グループ管理を行っています。この運用の便利なところはGitHub上のチームとサーバのアクセス権が一致させやすいことや、GitHubには公開鍵情報があり、そのまま利用できること、GitHubの情報をもとにしているので権限の剥奪漏れが少ないことです。</p>
<p>一方でファイル生成の方法はユースケースによって異なると思うので、この記事ではインストールから名前解決までを解説し、v2の変更点を紹介したいと思います。</p>
<p>各種ドキュメントは下記のサイトをご確認ください。</p>
<p><iframe class="hatenablogcard" style="width:100%;height:155px;margin:15px 0;max-width:680px;" title="STNS: Simple TOML Name Service" src="https://hatenablog-parts.com/embed?url=https://stns.jp/" frameborder="0" scrolling="no"></iframe></p>
<h2>サーバのインストール</h2>
<p>Rhel系、Debian系ともにパッケージを提供しています。この手順はUbuntu xenialで進めます。まずクライアントからのクエリに応答するサーバをインストールします。まずはパッケージリポジトリを追加します。</p>
<pre class="brush: bash; title: ; notranslate">

$ curl -fsSL https://repo.stns.jp/scripts/apt-repo.sh | sh

</pre>
<p>リポジトリをインストールしたらサーバをインストールします。</p>
<pre class="brush: bash; title: ; notranslate">

$ apt install -qqy stns-v2

</pre>
<p>次に設定ファイルを編集し、このようにしてみました。</p>
<pre class="brush: bash; title: ; notranslate">
$ cat /etc/stns/server/stns.conf
&#x5B;users.example]
id = 1001
group_id = 1001
keys = &#x5B;&quot;ssh-rsa XXXXX…&quot;]

&#x5B;groups.example]
id = 1001
users = &#x5B;&quot;example&quot;]
</pre>
<p>設定を編集したらrestartしてください。</p>
<pre class="brush: bash; title: ; notranslate">

$ service stns restart

</pre>
<p>curlを利用して動作確認しましょう。</p>
<pre class="brush: bash; title: ; notranslate">
$ curl -s http://localhost:1104/v1/users | jq
&#x5B;
  {
    &quot;id&quot;: 1001,
    &quot;name&quot;: &quot;example&quot;,
    &quot;password&quot;: &quot;&quot;,
    &quot;group_id&quot;: 1001,
    &quot;directory&quot;: &quot;&quot;,
    &quot;shell&quot;: &quot;&quot;,
    &quot;gecos&quot;: &quot;&quot;,
    &quot;keys&quot;: &#x5B;
      &quot;ssh-rsa XXXXX…&quot;
    ]
  }
]
</pre>
<p>たったこれだけでサーバの構築は完了です。次にクライアントをインストールします。</p>
<h2>クライアントのインストール</h2>
<p>クライアントもサーバと同じくパッケージからインストール可能です。</p>
<pre class="brush: bash; title: ; notranslate">

$ apt install -qqy libnss-stns-v2

</pre>
<p>サーバと同じように設定ファイルを書きます。</p>
<pre class="brush: bash; title: ; notranslate">
$ cat /etc/stns/client/stns.conf
api_endpoint     = &quot;http://localhost:1104/v1/&quot;
</pre>
<p>STNS自体の設定はこれで完了です。次に <code>nsswitch.conf</code> を下記のように編集し、Linuxの名前解決にSTNSを利用するようにします。下記のように <code>passwd</code>, <code>group</code>,<code>shadow</code>に<code>stns</code>を追加してください。</p>
<pre class="brush: bash; title: ; notranslate">
$ grep -e passwd -e shadow -e group /etc/nsswitch.conf
passwd:         compat stns
group:          compat stns
shadow:         compat stns
</pre>
<p>ここまでの作業でSTNSで名前解決ができるようになります。</p>
<pre class="brush: bash; title: ; notranslate">
$ id example
uid=1001(example) gid=1001(example) groups=1001(example)
</pre>
<h2>STNSを利用してSSHログインする</h2>
<p>STNSはLinuxのユーザー、グループの名前解決だけではなく、SSHの公開鍵ログインもサポートしています。公開鍵ログインを行うには、 <code>/etc/ssh/sshd_config</code> に下記の設定を行います。</p>
<pre class="brush: bash; title: ; notranslate">
PubkeyAuthentication yes
AuthorizedKeysCommand /usr/lib/stns/stns-key-wrapper
AuthorizedKeysCommandUser root
</pre>
<p>次にsshdをrestartします。</p>
<pre class="brush: bash; title: ; notranslate">
$ service ssh restart
</pre>
<p>テストするために公開鍵をSTNSに設定します。</p>
<pre class="brush: bash; title: ; notranslate">
$ mkdir /home/example &amp;&amp; chown example /home/example
$ su - example
example@localhost:~$ ssh-keygen
...
example@localhost:~$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCkokLd+sFWbKuqepOVNhStZA55MYQ+RiXiS9Ub1PhS5aPDh06MHf80oLg9mKoG883ZsjD7zgYKunUnafG3NdFqHHTgMLS3By0IPi3ZNKVZStvjvCEahqoAs7gUEzOuNqa9hW9f7f5BfausSSuJ1s00WglrPmCMLvfBf9Y54o3xZm+/Uw8ehFHi8s1x5p/7I25WlEwbkL24CmugvvKuWh/bIcK84xRJO43m2Q/u3IArQjOum8Xim+Psj1xDc6DkbToHmoVCnFDoyMUM+VjnLPYpE4gh/8DBFlzNnRi4FrahJMJGbG/wGP6uovbcbmoj46/nYXGMlzBvU3WnhZbOskf/ example@localhost

$ cat /etc/stns/server/stns.conf
&#x5B;users.example]
id = 1001
group_id = 1001
# ★作成した公開鍵を設定する
keys = &#x5B;&quot;ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCkokLd+sFWbKuqepOVNhStZA55MYQ+RiXiS9Ub1PhS5aPDh06MHf80oLg9mKoG883ZsjD7zgYKunUnafG3NdFqHHTgMLS3By0IPi3ZNKVZStvjvCEahqoAs7gUEzOuNqa9hW9f7f5BfausSSuJ1s00WglrPmCMLvfBf9Y54o3xZm+/Uw8ehFHi8s1x5p/7I25WlEwbkL24CmugvvKuWh/bIcK84xRJO43m2Q/u3IArQjOum8Xim+Psj1xDc6DkbToHmoVCnFDoyMUM+VjnLPYpE4gh/8DBFlzNnRi4FrahJMJGbG/wGP6uovbcbmoj46/nYXGMlzBvU3WnhZbOskf/&quot;]

&#x5B;groups.example]
id = 1001
users = &#x5B;&quot;example&quot;]

$ service stns restart
</pre>
<p>ここまででsshログインが可能になるので試してみます。</p>
<pre class="brush: bash; title: ; notranslate">
$ ssh localhost -l example
Last login: Sun Sep  9 02:11:43 2018 from ::1
example@localhost:~$
</pre>
<p>無事ログインできましたね。このように比較的少ない手番でLinuxの統合ユーザー管理が可能になるので、中小規模でお困りの方がいたらぜひ利用してみてください。またサーバサイドは普通のHTTPサーバなのでnginxやkeepalivedと組み合わせて冗長化することも可能ですし、Basic認証やToken認証に対応しているのでHTTPS化してセキュアな運用も可能です。</p>
<p>さて次にv1からの変更点をいくつか紹介します。</p>
<h1>v1からの変更点</h1>
<h2>nscdが不要</h2>
<p>従来のv1ではSTNS自体にキャッシュ機構がなかったため、nscdを利用したキャッシュが必要だったのですが、v2からは独自のキャッシュ機構が組み込まれているためnscdなしで高速動作が可能です。</p>
<h2>インターフェースがRESTfulに</h2>
<p>v1ではmapのようなデータ構造であったのですが、Arrayデータ構造へ変更し、よりサーバ側の開発が行いやすくなりました。またエンドポイントも全て見直しています。</p>
<h3>before</h3>
<pre class="brush: bash; title: ; notranslate">
{
  &quot;pyama&quot;: {
    &quot;id&quot;: 1001,
    &quot;group_id&quot;: 1001,
    &quot;directory&quot;: &quot;/home/pyama&quot;,
    &quot;shell&quot;: &quot;/bin/bash&quot;,
    &quot;gecos&quot;: &quot;&quot;,
    &quot;keys&quot;: &#x5B;
      &quot;ssh-rsa xxxx&quot;
    ]
  }
}

</pre>
<h3>afterr</h3>
<pre class="brush: bash; title: ; notranslate">
&#x5B;{
  &quot;name&quot;: &quot;pyama&quot;,
  &quot;id&quot;: 1001,
  &quot;group_id&quot;: 1001,
  &quot;directory&quot;: &quot;/home/pyama&quot;,
  &quot;shell&quot;: &quot;/bin/bash&quot;,
  &quot;gecos&quot;: &quot;&quot;,
  &quot;keys&quot;: &#x5B;
    &quot;ssh-rsa xxxx&quot;
  ]
}]
</pre>
<h2>libnss-stnsがGolangからC言語へ</h2>
<p>v1では全てGolangで開発されて追ったのですがv2からはクライアントサイドはC言語で開発されています。理由としてはv1はCGOを利用してクライアントサイドの処理を行っていたのですが、clone時のメモリの持ち回り方などが怪しく、SEGVの要因になっていたので、C言語で書き直しています。</p>
<h2>コードの見通しが良くなった</h2>
<p>もともとSTNSは筆者が2年ほど前にGolangの学習用途で開発したプロダクトだったのでどちらかというとバッドノウハウの塊のようなコードになっていました。それらを今の力ですべて書き直したので以前よりだいぶ見通しが良くなっています。</p>
<h2>パラメーター項目の変更</h2>
<ul>
<li>api_end_point → api_endpoint</li>
</ul>
<p>区切り文字の位置の変更及び、データが配列から、文字列へと変更されています。<code>["http://xxxx"]</code> → &#8220;http://xxxx&#8221;</p>
<h1>v1からの移行パス</h1>
<p>サーバ側はVirtualHostなりポートを変えるなりで起動すれば良さそうです。クライアント側は下記のように削除してから、インストールしてください。</p>
<pre class="brush: bash; title: ; notranslate">
$ apt remove libnss-stns
$ apt install libnss-stns-v2
</pre>
<h1>最後に</h1>
<p>公開から2年ほど経過し、GitHub Starは180を超え、商用サービスからお声がかかるほど成長した反面、コードベースがあまり良くなくメンテナンスするモチベーションが低くなっていtので、C言語の勉強を兼ねてフルスクラッチで書き直しました。サーバサイドもlabstack/echoを採用し、ミドルウェアを利用した拡張を行いやすくしています。</p>
<p>今後についてはSlackを利用した2FAや監査の仕組みなど機能拡張を進めていく予定です。まだ利用されてない方は利用していただいて、既に利用中の方はv2への移行を進めていただければ幸いです。</p>The post <a href="https://pyama.fun/archives/5985">Linuxユーザーと公開鍵を統合管理するサーバ&クライアントのSTNSを書き直した</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5985</post-id>	</item>
		<item>
		<title>gliderlabs/sshを利用してSSHをプラガブルに操作する</title>
		<link>https://pyama.fun/archives/5983</link>
		
		<dc:creator><![CDATA[pyama]]></dc:creator>
		<pubDate>Thu, 19 Jul 2018 16:44:38 +0000</pubDate>
				<category><![CDATA[go]]></category>
		<guid isPermaLink="false">https://ten-snapon.com/?p=1986</guid>

					<description><![CDATA[<p>今日帰る前に tsurubee3 と、SSHのコネクションアレコレできたら楽しそうじゃんみたいな話をしていて、gliderlabs/sshを利用したら割とシュッとできたので紹介します。 gliderlabs/sshはSS &#8230; </p>
<p class="link-more"><a href="https://pyama.fun/archives/5983" class="more-link">続きを読む<span class="screen-reader-text"> "gliderlabs/sshを利用してSSHをプラガブルに操作する"</span></a></p>
The post <a href="https://pyama.fun/archives/5983">gliderlabs/sshを利用してSSHをプラガブルに操作する</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></description>
										<content:encoded><![CDATA[<p>今日帰る前に <a href="https://twitter.com/tsurubee3">tsurubee3</a> と、SSHのコネクションアレコレできたら楽しそうじゃんみたいな話をしていて、<a href="https://github.com/gliderlabs/ssh">gliderlabs/ssh</a>を利用したら割とシュッとできたので紹介します。</p>
<p><code>gliderlabs/ssh</code>はSSHの認証や各ステップをミドルウェアパターンで拡張できるSSHサーバのライブラリです。今回は、このライブラリを利用して、SSHのパスワード認証をコンテキストに保持して使いまわしたり、PTYの入力を標準出力にパイプする実装を書いてみました。</p>
<p><iframe style="border:none;display:block;margin:0 0 1.7rem;overflow:hidden;height:155px;width:100%;max-width:100%;" title="pyama86/tssh: tsurubee ssh proxy server" src="//hatenablog-parts.com/embed?url=https://github.com/pyama86/tssh"></iframe></p>
<p>動作としてはこんな感じ。</p>
<h3>サーバ側</h3>
<pre class="brush: bash; title: ; notranslate">
% make server
==&gt; Installing Dependencies
go get -u github.com/golang/dep/...
dep ensure
go run main.go
</pre>
<h3>クライアント側</h3>
<pre class="brush: bash; title: ; notranslate">
% ssh localhost -p2222
yamashitakazuhiko@localhost's password:
</pre>
<p>パスワードは <code>test</code>にしておるので、入力した段階で、contextに保存され、サーバ側で下記のように表示されます。</p>
<pre class="brush: bash; title: ; notranslate">
user password: test
</pre>
<p>あとは、クライアント側でタイプした内容が次々と標準出力に出る感じです。</p>
<pre class="brush: bash; title: ; notranslate">
user password: testpyama saikou
</pre>
<p>改行してないからあれな感じになっちゃいましたね・・・。</p>
<p>Golang、標準ライブラリやそれを利用したミドルウェア実装が豊富で、アイディアさえれば何でも出来ちゃう状況にあるので、こういうおもしろハックを突き詰めていくと事業を差別化する技術シュッと作れそうで便利な世の中になったなぁと思いました。</p>
<p>ただこの実装だとSCPがカバーできてないのと、SCPのGolang実装あまりなさそうだから、RFP読むなり、sshd読むなりして実装すると楽しそう。</p>The post <a href="https://pyama.fun/archives/5983">gliderlabs/sshを利用してSSHをプラガブルに操作する</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5983</post-id>	</item>
		<item>
		<title>gothreeというサーバのログをS3にアップロードするコマンドを書いた</title>
		<link>https://pyama.fun/archives/5980</link>
		
		<dc:creator><![CDATA[pyama]]></dc:creator>
		<pubDate>Fri, 08 Jun 2018 09:21:42 +0000</pubDate>
				<category><![CDATA[go]]></category>
		<guid isPermaLink="false">https://ten-snapon.com/?p=1957</guid>

					<description><![CDATA[<p>ペパボのサービスで利用しているサーバにおいて、ログの保存期間を長期化するに当たり、超長期の保管場所をS3に選定した。世の中ではSHELLスクリプト書いてアップロード処理が実装されている例が多そうなのだが、awscliに依 &#8230; </p>
<p class="link-more"><a href="https://pyama.fun/archives/5980" class="more-link">続きを読む<span class="screen-reader-text"> "gothreeというサーバのログをS3にアップロードするコマンドを書いた"</span></a></p>
The post <a href="https://pyama.fun/archives/5980">gothreeというサーバのログをS3にアップロードするコマンドを書いた</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></description>
										<content:encoded><![CDATA[<p><iframe style="border:none;display:block;margin:0 0 1.7rem;overflow:hidden;height:155px;width:100%;max-width:100%;" title="pyama86/gothree: Back up to s3 when log rotation with golang" src="//hatenablog-parts.com/embed?url=https://github.com/pyama86/gothree"></iframe></p>
<p>ペパボのサービスで利用しているサーバにおいて、ログの保存期間を長期化するに当たり、超長期の保管場所をS3に選定した。世の中ではSHELLスクリプト書いてアップロード処理が実装されている例が多そうなのだが、awscliに依存したくない気持ちがあったのでGolangで実装した。</p>
<p>gzipに自動で圧縮したりdateextを自動で付与したり、いい感じにアップロードされるようになっている。使用方法はAWSのIDなどを環境変数かオプションで渡してあげて、logrotateの設定のpostrotateに定義するだけで動く。</p>
<pre class="brush: bash; title: ; notranslate">
$ cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
  daily
  missingok
  rotate 52
  compress
  delaycompress
  notifempty
  create 640 nginx adm
  sharedscripts
  postrotate
    if &#x5B; -f /var/run/nginx.pid ]; then
            kill -USR1 `cat /var/run/nginx.pid`
    fi
    ★これだけでOK
    /bin/bash -c -l &amp;quot;source /root/.aws &amp;amp;&amp;amp; /usr/local/bin/gothree $1&amp;quot;
  endscript
}
$ cat /root/.aws
export AWS_ACCESS_KEY_ID=***********
export AWS_SECRET_ACCESS_KEY=***********
export AWS_REGION=your region
export AWS_BUCKET=your buket name
</pre>
<p>作成されるファイル名はこんな感じ。</p>
<p><a href="https://ten-snapon.com/wp-content/uploads/2018/06/baddd6ea57766a548490535eb9743712.png"><img loading="lazy" decoding="async" src="https://ten-snapon.com/wp-content/uploads/2018/06/baddd6ea57766a548490535eb9743712-1024x212.png" alt="" width="1000" height="207" class="aligncenter size-large wp-image-1961" /></a></p>
<p>昨今セキュリティの観点から保全範囲が広がってきていると思うので、ぜひ活用してみてください。</p>The post <a href="https://pyama.fun/archives/5980">gothreeというサーバのログをS3にアップロードするコマンドを書いた</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5980</post-id>	</item>
		<item>
		<title>Apache httpd + PHPな環境で閲覧できるファイルを調べるpmrというコマンドを開発した</title>
		<link>https://pyama.fun/archives/1821</link>
		
		<dc:creator><![CDATA[pyama]]></dc:creator>
		<pubDate>Sat, 24 Feb 2018 02:09:13 +0000</pubDate>
				<category><![CDATA[go]]></category>
		<guid isPermaLink="false">https://ten-snapon.com/?p=1821</guid>

					<description><![CDATA[<p>このブログの対象読者 Apache httpd + PHPな環境で、本番運用をしているプログラマ、インフラエンジニア。 このブログが解決すること Apache httpd + PHPな環境ではファイルの配置や、サーバの設 &#8230; </p>
<p class="link-more"><a href="https://pyama.fun/archives/1821" class="more-link">続きを読む<span class="screen-reader-text"> "Apache httpd + PHPな環境で閲覧できるファイルを調べるpmrというコマンドを開発した"</span></a></p>
The post <a href="https://pyama.fun/archives/1821">Apache httpd + PHPな環境で閲覧できるファイルを調べるpmrというコマンドを開発した</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></description>
										<content:encoded><![CDATA[<h1>このブログの対象読者</h1>
<p>Apache httpd + PHPな環境で、本番運用をしているプログラマ、インフラエンジニア。</p>
<h1>このブログが解決すること</h1>
<p>Apache httpd + PHPな環境ではファイルの配置や、サーバの設定によっては、想定外のファイルにアクセスされることがあります。それは例えば<em>password.conf</em> のようなファイルが閲覧されたり、任意のphpファイルが意図しない実行をされたりすることにつながります。このブログでは、そういったアクセスが可能であるかどうかをチェックする手段を提供します。</p>
<h1>pmr</h1>
<p><iframe style="border:none;display:block;margin:0 0 1.7rem;overflow:hidden;height:155px;width:100%;max-width:100%;" title="pyama86/pmr: pyama multi request" src="//hatenablog-parts.com/embed?url=https://github.com/pyama86/pmr"></iframe></p>
<p>pmrとは筆者が開発した、コマンドで、 <em>Pyama Multi Request</em> の略です。アーキテクチャとしては、標準入力から受けたファイルリスト + URLで並列的にアクセスを行い、レスポンスボディの10行とファイルの10行を比較し、一致している場合に、そのファイルが見えていると判定します。</p>
<p>例を示します。</p>
<pre class="brush: bash; title: ; notranslate">
$ wget https://pepabo.com
...
2018-02-24 10:35:47 (59.7 MB/s) - ‘index.html’ saved &#x5B;33941]
$ echo index.html | pmr -url https://pepabo.com
INFO&#x5B;0000] request: https://pepabo.com 200 OK
INFO&#x5B;0000] request: https://pepabo.com/index.html 200 OK
WARN&#x5B;0000] This file is published index.html
</pre>
<p>↑の実行例では、 <em>https://pepabo.com</em> にアクセスし、<em>index.html</em> を保存後、<em>pmr</em> に対して、標準入力から <em>index.html</em>を渡して実行しています。 <em>pmr</em> は <em>https://pepabo.com/index.html</em> のように連結したURLでhttpアクセスを行い、レスポンスボディを比較した結果、内容が一致しているので、 <code>WARN[0000] This file is published index.html</code> のようにwarning表示を行っています。</p>
<p>また、応答が <em>HTTP StatusOK(200)</em> ではない場合もwarning表示が行われるので、意図しないphpにアクセスされて、 <em>HTTP IntrenalServerError(500)</em>などの場合も検知することが可能です。</p>
<h1>pmrの具体的な使い方</h1>
<p>pmrを実行するのは、WEBサービスのリポジトリのDocumentRootに相当するディレクトリで下記のように実行することを想定しています。</p>
<pre class="brush: bash; title: ; notranslate">
$ pwd
/path/to/service/public
$ find ./ -type f |grep -v -e '\.gif|\.png|\.jpg|\.js|\.css|\.html|\.ico|\.svg|\.pdf' | \
pmr -u https://example.com  2&gt;&amp;1 | grep WARN
# passwordっぽいのが見えっちゃってる
WARN&#x5B;0000] This file is published password.html
</pre>
<p>このように <em>pmr</em> では標準出力からファイルリストをもらうことで、柔軟なアクセスを実現しています。例えば、本番サーバのDocumentRootからファイルリストを持ってくることで、ログファイルなどの本番サーバにしか無いファイルも調査することが可能になるでしょう。</p>
<p><img decoding="async" src="https://raw.githubusercontent.com/pyama86/capture/master/2018-02-24T11%3A04%3A44.png" alt="pmr" /></p>
<h1>最後に</h1>
<p>昨今のWEBシステムにおいては、WEBアプリケーションフレームワークに則って開発すると、意図しないファイルの公開は限りなく少なくなると思いますが、黎明期の手違いや、複数人で開発している状況だと、意図しないファイルの公開が行われているかもしれません。そういったことを自動でチェックする仕組みとして <em>pmr</em> を是非活用いただければ嬉しいです。</p>The post <a href="https://pyama.fun/archives/1821">Apache httpd + PHPな環境で閲覧できるファイルを調べるpmrというコマンドを開発した</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1821</post-id>	</item>
		<item>
		<title>スクリーンショットをgithub.comやGHEで管理する</title>
		<link>https://pyama.fun/archives/1796</link>
		
		<dc:creator><![CDATA[pyama]]></dc:creator>
		<pubDate>Sat, 10 Feb 2018 13:35:02 +0000</pubDate>
				<category><![CDATA[go]]></category>
		<guid isPermaLink="false">https://ten-snapon.com/?p=1796</guid>

					<description><![CDATA[<p>このブログの対象読者 Gyazoがセキュリティポリシー上利用できず、また社内Gyazoなども何らかの理由で利用できないような環境で、GitのWikiや、ソースのREADMEなどに画像を利用するときに、一度issueに画像 &#8230; </p>
<p class="link-more"><a href="https://pyama.fun/archives/1796" class="more-link">続きを読む<span class="screen-reader-text"> "スクリーンショットをgithub.comやGHEで管理する"</span></a></p>
The post <a href="https://pyama.fun/archives/1796">スクリーンショットをgithub.comやGHEで管理する</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">このブログの対象読者</h1>



<p>Gyazoがセキュリティポリシー上利用できず、また社内Gyazoなども何らかの理由で利用できないような環境で、GitのWikiや、ソースのREADMEなどに画像を利用するときに、一度issueに画像を貼り付けたりしたりするのがうんざりな人。</p>



<h1 class="wp-block-heading">このブログの内容で何ができるようになるか</h1>



<p>スクリーンショットの画像がgithub.com、GHEの指定したリポジトリに自動でアップロードされ、クリップボードに所謂マークダウンの画像表示記法 <code>![](画像URL)</code> がコピーされるようになります。副次的に過去のスクリーンショットが自動でGit管理されるようになるので、過去に撮ったデータを探すのが便利になったりします。</p>



<h1 class="wp-block-heading">手順</h1>



<h2 class="wp-block-heading">nrmをダウンロードする</h2>



<figure><iframe style="border: none; display: block; margin: 0 0 1.7rem; overflow: hidden; height: 155px; width: 100%; max-width: 100%;" title="pyama86/nrm: Upload the screenshot to github. Then, embed tags in your clipboard.(jp:クリップボードの画像をGithubにアップロードして、貼り付けタグをクリップボードに埋め込みます)" src="//hatenablog-parts.com/embed?url=https://github.com/pyama86/nrm"></iframe></figure>



<p>github.comのreleaseから最新のバイナリをダウンロードして、 <code>/usr/local/bin/</code> あたりにおいてください。</p>



<pre class="wp-block-preformatted"><pre class="brush: bash; title: ; notranslate">
$ wget https://github.com/pyama86/nrm/releases/download/v0.1.0-d21aa37/darwin_amd64.zip
$ unzip darwin_amd64.zip
$ mv nrm /usr/local/bin
</pre></pre>



<h2 class="wp-block-heading">Automatorでシェルスクリプトとしてサービス登録する</h2>



<p>Automatorを利用すると、シェルスクリプトをサービスとして登録したり、色々なワークフローを定義することが出来ます。</p>



<figure class="wp-block-image"><img decoding="async" src="https://raw.githubusercontent.com/pyama86/capture/master/2018-02-10T21%3A49%3A27.png" alt="automator"/></figure>



<p>まずサービスとしてnrmを登録します。</p>



<figure class="wp-block-image"><img decoding="async" src="https://raw.githubusercontent.com/pyama86/capture/master/2018-02-10T22%3A09%3A45.png" alt="service"/></figure>



<p>シェルスクリプトを実行を検索して選択</p>



<figure class="wp-block-image"><img decoding="async" src="https://raw.githubusercontent.com/pyama86/capture/master/2018-02-10T22%3A10%3A50.png" alt="shell"/></figure>



<p><strong>サービスは次の項目を受け取ります</strong> の項目は <strong>入力なし</strong> であとは下記の感じで入力します。<br />もしGHEを利用する場合は、トークンに加えて <code>GITHUB_API_ENDPOINT</code> も指定してください。また環境変数を <code>.zshenv</code> などで指定している場合は、ここでの指定は不要です。各項目の意味は <code>-o</code> はリポジトリのオーナー、 <code>-r</code> はリポジトリを指定しています。この場合、僕のgithub.comアカウントのcaptureというリポジトリに画像データが送信されます。</p>



<figure class="wp-block-image"><img decoding="async" src="https://raw.githubusercontent.com/pyama86/capture/master/2018-02-10T22%3A12%3A10.png" alt="zsh"/></figure>



<pre class="wp-block-code"><code>GITHUB_API_ENDPOINT=https://your.ghe.local/api/v3/
GITHUB_AUTH_TOKEN=xxxxxxxxxxxxxxxx
/usr/local/bin/nrm -o &lt;organization> -r &lt;repository></code></pre>



<p>入力ができたら <code>nrm_shell</code> として登録してください。</p>



<p></p>



<figure class="wp-block-image"><img decoding="async" src="https://raw.githubusercontent.com/pyama86/capture/master/2018-02-10T22%3A14%3A14.png" alt="nrm_shell"/></figure>



<h2 class="wp-block-heading">キーボードショートカットを設定する</h2>



<p>登録した<code>nrm_shell</code>はショートカットを定義することでどこからでも呼び出すことが出来ます。</p>



<p><code>[システム環境設定]</code>-&gt;<code>[キーボード]</code>-&gt;<code>[ショートカット]</code>-&gt;<code>[サービス]</code></p>



<p>下までスクロールすると <code>nrm_shell</code> があるので、ショートカットを割り当ててください。<br />僕の場合は <code>Command + Alt + n</code> で起動するようにしています。</p>



<figure class="wp-block-image"><img decoding="async" src="https://raw.githubusercontent.com/pyama86/capture/master/2018-02-10T22%3A30%3A19.png" alt="alt"/></figure>



<p>ここまでの作業が終わると、ショートカットを入力するだけで、いつでもスクリーンショットがgithub.comに送信されるようになります。</p>



<h1 class="wp-block-heading">まとめ</h1>



<p>それなりに限られた環境で力を発揮する <code>nrm</code> コマンドですが、このブログを書くにあたり自分で利用してみたところ、めちゃくちゃ便利だったので、ブロガーの方々も活用いただけるかもしれません。</p>



<p>P.S.<br />nrmコマンドの由来は、雰囲気で同僚の名字から取りました。</p>The post <a href="https://pyama.fun/archives/1796">スクリーンショットをgithub.comやGHEで管理する</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1796</post-id>	</item>
		<item>
		<title>NetAPPのquotaを自動で適用するデーモンを開発した@福岡Ruby会議</title>
		<link>https://pyama.fun/archives/5973</link>
		
		<dc:creator><![CDATA[pyama]]></dc:creator>
		<pubDate>Sat, 25 Nov 2017 05:31:44 +0000</pubDate>
				<category><![CDATA[go]]></category>
		<guid isPermaLink="false">https://ten-snapon.com/?p=1715</guid>

					<description><![CDATA[<p>皆さん！福岡Ruby会議02楽しんでますか！？ 僕はというもののCFP期限を逃し、全く何も生み出していないので、セッションの合間にデーモンを書いていました。 NetAPPではqtreeやVolumeに対してファイル数やデ &#8230; </p>
<p class="link-more"><a href="https://pyama.fun/archives/5973" class="more-link">続きを読む<span class="screen-reader-text"> "NetAPPのquotaを自動で適用するデーモンを開発した@福岡Ruby会議"</span></a></p>
The post <a href="https://pyama.fun/archives/5973">NetAPPのquotaを自動で適用するデーモンを開発した@福岡Ruby会議</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></description>
										<content:encoded><![CDATA[<p>皆さん！福岡Ruby会議02楽しんでますか！？<br />
僕はというもののCFP期限を逃し、全く何も生み出していないので、セッションの合間にデーモンを書いていました。</p>
<p><iframe style="border:none;display:block;margin:0 0 1.7rem;overflow:hidden;height:155px;width:100%;max-width:100%;" title="pyama86/netapp-quota: Apply NetApp quarter" src="//hatenablog-parts.com/embed?url=https://github.com/pyama86/netapp-quota"></iframe></p>
<p>NetAPPではqtreeやVolumeに対してファイル数やディスク容量のクォータを設定することが出来ますが、クォータを設定するだけでは有効にならないので、一度クォータをVolume単位でOFF，ONして上げる必要があります。</p>
<p>今回開発したデーモンを利用すると、指定秒数間隔でクォータをOFFし、指定秒数間隔でクォータがONされます。それぞれの処理はgoroutineで並列実行されるため、300秒間隔で、OFFし、10秒間隔でONするようにすると実質5分毎にクォータが有効になるような実装です。</p>
<p>例外時は停止するよりも通知だけ行い、処理は継続すべきと考え、エラーログを出力し、Slack通知だけして処理は継続する設計です。実運用時はsystemdなどと組み合わせて利用するのがよいと思います。</p>
<p>このあとは @k1LoW さんと @matsumotory の発表が被って悩ましい時間になってきましたが引き続きRuby会議やっていきましょう！<br />
MockMock!!!1</p>The post <a href="https://pyama.fun/archives/5973">NetAPPのquotaを自動で適用するデーモンを開発した@福岡Ruby会議</a> first appeared on <a href="https://pyama.fun">pyama.fun</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5973</post-id>	</item>
	</channel>
</rss>
