Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Push source to GitHub/GitLab repository without type password

Background

The passphrase stored in ssh-agent is invalidated when the git client machine is rebooted.

If you want to access GitHub/GitLab without type password (e.g. push/pull source in Jenkins), you have to use a ssh public/public key pair without passphrase, rather than ssh-agent.

Process

1. Make a key pair for GitHub/GitLab

Make a new key pair with ssh-keygen. You can use ssh-keygen -t ecdsa, or ssh-keygen -t ed25519 for more security. In GitLab, ssh-keygen -t ed25519 is acceptable on GitLab 10.3 or later.

$ ssh-keygen -t rsa

For convenience, make a new public/public key pair (~/.ssh/id_rsa_nopassphrase) without passphrase.

Generating public/private rsa key pair.
Enter file in which to save the key (/home/yourname/.ssh/id_rsa): /home/yourname/.ssh/id_rsa_nopassphrase

For passphrase prompt, just type enter twice (no passphrase).

Enter passphrase (empty for no passphrase): 
Enter same passphrase again:

ssh-keygen will make a new key pair.

Your identification has been saved in /home/yourname/.ssh/id_rsa_nopassphrase.
Your public key has been saved in /home/yourname/.ssh/id_rsa_nopassphrase.pub.
The key fingerprint is:
SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX yourname@Hostname

The key's randomart image is:
+---[RSA 2048]----+
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
+----[SHA256]-----+

2. Set the new public key to GitHub/GitLab

Open the new public key (~/.ssh/id_rsa_nopassphrase.pub) and copy&paste the content.

  • If you use GitLab, Open 'SSH Keys' tab in 'Profile Settings', and paste the key in the 'Key' section.
  • If you use GitHub, Open 'SSH and GPG keys' tab in 'Settings', push 'New SSH key', and paste the key in the 'Key' section.

3. Make git to use the new private key

Make ~/.ssh/config file if it isn't exist. add the content below to the ~/.ssh/config. This setting makes git to use the new private key for ssh connection.

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_nopassphrase
    Port 22

4. Test the connection to GitHub/GitLab

$ ssh -T git@github.com
Hi yourname! You've successfully authenticated, but GitHub does not provide shell access.

If any error occurred, try ssh -Tv git@github.com, and see the messages.

5. Try git command

Note that use the username git (not your account name in GitHub/GitLab) in comnand.

$ git clone --depth 1 git@github.com:yourname/repository.git
$ cd repository/
$ git push
Everything up-to-date

How to escape special characters in PowerShell

Ways to escape special characters in PowerShell are different between its context, and so complecated...

# Context Escaped char " ' \ ` ``
1 String with " "
2 String with " $
3 String with " '
4 String with " `
5 String with ' "
6 String with ' $
7 String with ' '
8 String with ' `
9 Operand of -like with " *
10 Operand of -like with ' *
11 Operand of -match with " *
12 Operand of -match with ' *
13 Placeholder of -F operator *1 { and }

legend

  • ✓ … You can use it to escape
  • ✘ … You cannot use it to escape
  • - … You don't have to escape

キーノートとプレゼンと卒論発表の違い

卒論発表なのにAppleのキーノートみたいになっているケース、この辺を取り違えているのかなぁ、という印象。

キーノート プレゼン 論文(卒論とか)発表
聞く人は話の内容に興味があるの? 興味があるから聞きに来てる 商品やサービスがよく分からないから話を聞いてる 興味があるもないもそれで食ってる
聞く人の人数は? とても多い だいたいあまり多くない だいたいあまり多くない
聞く人の知識は? 人によって知識はバラバラ 商品やサービスそのものとは別に、何か専門分野を持っていることが多い 発表される内容の専門家
発表者は何に注目してほしい? このカンファレンス自体(またはそこで発表される商品)がいかに魅力的か プレゼンで直接紹介している商品やサービスがいかに魅力的か 発表の自体の内容が妥当か
発表の目的は? カンファレンスを盛り上げること 商品やサービスに興味を持ってもらうこと 発表者自身が卒業等に足る知識を持っていると証明すること

Cupheadの元ネタ、あまり触れられていなさそうなところ

Super AttacksのGiant Ghost
これは妖怪道中記のご先祖様じゃないのかなぁ……
Wally Warbles (Phase 1)
グラディウスII (VULCAN VENTURE)のステージ1ボス(フェニックス)っぽい。「ドンキーコング リターンズ」が元ネタじゃないの、とも言われてるみたいだけど……
Wally Warbles (Phase 2)
グラディウスIII (Gradius III) のステージ5ボス(ドガス)、またはパロディウスだ!のステージ1ボス(キャプテンペンギンノフスキーIII)
Pirouletta
極上パロディウス (Fantastic Journey) のステージ1ボス(アンナ・パブロワ&めろーら)では。少なくとも回転して弾をバラまくところはそうだと思うんだけど。
Cagney Carnation
これは怪しいけど、足場の下から上向きにツタを伸ばしてくるあたりが、アクトレイザー (ActRaiser) マラーナAct1のボス(ラフレシア)を思いだすんだよね。あと、このステージの足場はロックマン2 (Mega Man 2) のアイテム1号では。

Invoke-RestMethod to GitLab API causes mojibake

Context

In PowerShell, You can call REST API with Invoke-RestMethod like:

Invoke-RestMethod -Headers $headers -Method Get -Timeout 10 -Uri "https://api.github.com/users/octocat/orgs"

Problem

The result of Invoke-RestMethod causes mojibake when the response contains non-ASCII characters like Japanese/Chinese characters.

Reason

  • Invoke-RestMethod treats the charset of the HTTP response as ISO-8859-1 rather than UTF-8, when the response doesn't have Content-Type: ... charset=utf-8 in HTTP header.
  • The response of GitLab API doesn't have Content-Type HTTP header.

Solution

Use Invoke-WebRequest rather than Invoke-RestMethod. You can decode the content of HTTP response as UTF-8 explicitly.

$res = (Invoke-WebRequest -Headers $headers -Method Get -Timeout 10-Uri "https://gitlab.example.com/api/v4/projects")
$con = [System.Text.Encoding]::Utf8.GetString([System.Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($res.Content))
ConvertFrom-Json $con