@kyanny's blog

My thoughts, my life. Views/opinions are my own.

Bash 現在の編集モードを確認する

  1. set -o
  2. shopt -o
  3. bind -V | grep editing-mode

emacs - How to set and determine the command-line editing mode of Bash? - Unix & Linux Stack Exchange

set -o

bash-3.2$ set -o
allexport       off
braceexpand     on
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off

shopt -o

bash-3.2$ shopt -o
allexport       off
braceexpand     on
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off

bind -V

bash-3.2$ bind -V
bind-tty-special-chars is set to `on'
blink-matching-paren is set to `on'
byte-oriented is set to `off'
completion-ignore-case is set to `off'
convert-meta is set to `off'
disable-completion is set to `off'
enable-keypad is set to `off'
expand-tilde is set to `off'
history-preserve-point is set to `off'
horizontal-scroll-mode is set to `off'
input-meta is set to `on'
mark-directories is set to `on'
mark-modified-lines is set to `off'
mark-symlinked-directories is set to `off'
match-hidden-files is set to `on'
meta-flag is set to `on'
output-meta is set to `on'
page-completions is set to `on'
prefer-visible-bell is set to `on'
print-completions-horizontally is set to `off'
show-all-if-ambiguous is set to `off'
show-all-if-unmodified is set to `off'
visible-stats is set to `off'
bell-style is set to `audible'
comment-begin is set to `#'
completion-query-items is set to `100'
editing-mode is set to `emacs'
keymap is set to `emacs'
bash-3.2$ bind -V | grep -E 'editing-mode|keymap'
editing-mode is set to `emacs'
keymap is set to `emacs'
  • shopt -o は set -o と同じ?
  • bind -V で見えるのは readline の変数

Zsh の場合は ZLE (Zsh Line Editor) という機能?がコマンド行の編集に使われているので、上記の設定は無関係っぽい。

zsh: 18 Zsh Line Editor

マニュアル読んでも現在の設定内容の調べ方がわからなかった。変更方法は bindkey -e bindkey -v

http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Keymaps

In addition to these names, either ‘emacs’ or ‘viins’ is also linked to the name ‘main’. If one of the VISUAL or EDITOR environment variables contain the string ‘vi’ when the shell starts up then it will be ‘viins’, otherwise it will be ‘emacs’. bindkey’s -e and -v options provide a convenient way to override this default choice.

とあるので、「デフォルトは emacs キーバインド」ということで良いのだろう。

Signing HTTP Messages について

このブログ記事の説明が網羅的でわかりやすい。

仕様は移り変わっており、版も何度も変更されている。ライブラリや API がどの版に基づいているかによって相性問題が発生する。新しい仕様に基づいたライブラリで署名したリクエストは古い仕様に基づいていると思われる API に拒否される、ということが起こる(起こった)。仕様の URL も何種類かあり、紛らわしい。

https://github.com/w3c-ccg/http-signatures このリポジトリで管理されていたが現在は https://github.com/httpwg/http-extensions に移動したようだ。

Ruby 実装だと https://github.com/99designs/http-signatures-ruby というのがある。試したところちゃんと使えた。試してないが https://github.com/bolmaster2/http-signature というのもあり、こちらも使えそうな気がする。 https://github.com/krainboltgreene/net-http-signature.gem というのも試したが、これは使えなかった(Base64.strict_encode64 してなかったり、論外な印象)。

他言語の実装は https://github.com/topics/http-signature, https://github.com/topics/http-signatures などから探せる。上記の Gem のリポジトリが載ってないのはリポジトリの Topics が未設定なためらしい。 Topics 設定はオーナーしかできず、 Pull Request を送ることもできない。

いくつかハマったところなど

  • シグネチャが不正な場合サーバーは 401 Unauthorized とともに WWW-Authenticate: Signature realm="Example",headers="(request-target) (created)" のようなヘッダを返す仕様になっている。 headers= のところにシグネチャ値の計算に用いるべきヘッダ名を入れることになっているようだが、実際に必要なヘッダは (request-target) host date なのに (request-target) host date content-type content-length を返すサーバがいたりする
  • シグネチャの計算にリクエストボディそのものを含めることを要求するサーバがいたりする。ライブラリによっても対応はバラバラで、リクエストボディのコンテンツは含めず Content-Length だけをシグネチャ計算に用いることが決め打ちで実装されていたりする
  • クライアントは Authorization: Signature keyId="rsa-key-1",algorithm="hs2019", headers="(request-target) (created) host digest content-length", signature="Base64(RSA-SHA512(signing string))" のようなリクエストヘッダを送ることになっているが、昔の仕様では algorithm="hmac-sha256" のようにシグネチャ計算に用いるアルゴリズム名を入れることになっていたのが、セキュリティ対策だかで途中から algorithm="hs2019" という決め打ちの値を入れる仕様に変更され、アルゴリズム名は keyId="rsa-key-1" という風に入れることになったようだ。しかしサーバ実装の中にはアルゴリズムを algorithm="" から取得する実装になっているものがあったりする(keyId="" のところにアルゴリズム名ではなく API KEY を入れる仕様になっていたりする)

ハマったことをふまえた所感

RSA とか HMAC とかの難しい計算の部分はどうせ各言語の標準ライブラリ等を使えるので、 Signing HTTP Messages の仕様を満たすための実装はそれほど複雑でもない。シグネチャを計算して所定の書式のヘッダに詰め込むだけ。ライブラリやサーバの実装のばらつき具合を見ると、相性問題に頭を悩ませながら使えるライブラリを探したり使い方を工夫したりする手間をかけるよりも、自前で実装してしまった方が早いしトラブルも少ない気がする。

漫画バビロン大富豪の教え 「お金」と「幸せ」を生み出す五つの黄金法則

黄金に愛される七つ道具

  1. 収入の十分の一を貯金せよ
  2. 欲望に優先順位をつけよ
  3. 貯えた金に働かせよ
  4. 危険や天敵から金を堅守せよ
  5. より良きところに住め
  6. 今日から未来の生活に備えよ
  7. 自分こそを最大の資本にせよ

「お金」と「幸せ」を生み出す五つの黄金法則

  1. 家族と自分の将来のために収入の十分の一以上を蓄える者の元には黄金は自らを膨らませながら、喜んでやってくるだろう
  2. 黄金に稼げる勤め先を見つけてやり持ち主が群れを膨大に増やす羊飼いのように賢明ならば黄金は懸命に働くことだろう
  3. 黄金の扱いに秀でた者の助言に熱心に耳をかたむける持ち主からは、黄金が離れることはないだろう
  4. 自分が理解していない商い、あるいは、黄金の防衛に秀でた者が否定する商いに投資してしまう持ち主からは黄金は離れていくだろう
  5. 非現実的な利益を出そうとしたり謀略化の甘い誘惑の言葉にのったり己の未熟な経験を盲信したりする者からは黄金は逃げることになるだろう