Go言語 開発整備 on MacOSX

環境

  • MacOSX 10.10.4 Yosemite
  • Go 1.4
  • Eclipse Mars Release (4.5.0)

概要

2015-08-01時点

IntelliJでgo開発を検討していましたが
Go言語Ver 1.4 をサポートしていなかったので
Eclipseに GoClipse を入れて補間機能等を整備します。

Go インストール

1
$ brew install go

brewをインストールしていない場合は以下参照

https://kenzo0107.github.io/2015/02/27/2015-02-28-install-homebrew-on-macosx/

Go バージョン確認

1
2
$ go version
go version go1.4.2 darwin/amd64

環境変数設定

個人的には ~/.zshrc 利用していますが、ない場合は
~/.bash_profile などに以下を追記してください。

1
2
3
4
5
# go
if [ -x "`which go`" ]; then
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
fi

ちなみに -x は実行可能か判定しています。
すなわち「if [ -x “which go“]」 は which go というコマンドが実行できるかを判定しています。

現状ローカル環境で which go と入力すると
以下のようになります。

1
2
$ which go
/usr/local/bin/go

GOPATH

  • Workspaceになります。プロジェクトはこちらに作っていくことになります。
  • go install, go get した際の保存場所になります。

go 環境情報確認

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ go env

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/kenzo/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.4.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.4.2/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"

brewgoをインストールした場合、GOROOTは /usr/local/Cellar/ 配下となります。

環境変数設定反映

.zshrc の場合

1
$ source .zshrc

.bash_profile の場合

1
$ source .bash_profile

GoClipseインストール

Eclipse の上部メニューの Help > Install New Software... クリック

GoClipse Software Location設定

GoClipse を選択しインストール

  • GoClipseにチェックを入れ Next >ボタンクリックでインストールを進めてください。

  • Perspective に Go が表示されるようになります。

GoClipse 各パス設定

以上

Eclipse でGoコードデバッグツールgdb設定 on MacOSX

概要

goのデバックモジュール GDBをインストールします。
ただMacのセキュリティ上の理由からGDBを利用するには
証明書を作成する必要があります。

環境

  • MacOSX 10.10.4 Yosemite
  • Go 1.4
  • Eclipse Mars Release (4.5.0)
  • gdb 7.9

GDB インストール

1
$ brew install homebrew/dupes/gdb

GDB バージョン確認

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ gdb --version
GNU gdb (GDB) 7.9
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin14.1.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".

証明書 作成

gdbgdb-cert の署名を適用

1
2
$ codesign -s gdb-cert /usr/local/Cellar/gdb/7.9/bin/gdb        [master]
gdb-cert: ambiguous (matches "gdb-cert" and "gdb-cert" in /Library/Keychains/System.keychain)

taskgated プロセスをkill

1
$ sudo killall taskgated

MacOSX 再起動後以下確認

以下のように C/C++GDB が表示される

以上

Boot2DockerでMacOSXローカル環境に開発環境構築

DockerEngine環境構築

開発環境

  • MacOSX Yosemite 10.3.3
  • VirtualBox 4.3.28
  • Vagrant 1.7.2

Dockerを利用するには以下が必要

Docker Engine

  • Dockerクライアント
    • docker コマンド実行
  • Dockerサーバ
    • Dockerコンテナ実行

Docker Engine 構成

  • DockerサーバとクライアントはRESTfulなHTTPSで通信する
1
2
3
4
5
6
7
8
9
                        +---------+ +----------+
|Docker | |Docker |
|Container| |Container |
+---------+ +----------+
↑ ↑
+-----------------+ +---------------+
| Docker Client | ----> | Docker Server |
|(docker Command) | +---------------+
+-----------------+

上記を構築するために以下をインストールする

Boot2Docker

  • DockerクライアントとDockerサーバをまとめてセットアップできるソフトウェア

Boot2Docker構成

  • Docker Client
  • Linux VM
  • Docker Server
1
2
3
4
5
|               | Docker Server |
| | Linux VM |
| Docker Client | VirtualBox |
+-------------------------------+
| Mac OSX or Windows |

導入手順

Boot2Docker 公式サイト : より 「MacOSXボタン」クリック

Boot2Dockerインストール

Boot2Docker パッケージインストール

Boot2Dockerインストーラ

インストーラを実行

Imgur

Imgur

Terminal等で以下実行
  • Linux VM作成
1
$ boot2docker init
  • Linux VM起動
1
2
3
4
5
6
7
8
9
10
11
12
13
$ boot2docker start

Waiting for VM and Docker daemon to start...
...........................ooooooooooooooooo
Started.
Writing /Users/kenzo/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/kenzo/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/kenzo/.boot2docker/certs/boot2docker-vm/key.pem

To connect the Docker client to the Docker daemon, please set:
export DOCKER_HOST=tcp://192.168.59.103:2376
export DOCKER_CERT_PATH=/Users/kenzo/.boot2docker/certs/boot2docker-vm
export DOCKER_TLS_VERIFY=1
  • Linux VMステータス確認
1
2
$ boot2docker status
running
  • 環境変数の設定

boot2docker start にて起動時にexport設定を実行する

1
2
3
$ export DOCKER_HOST=tcp://192.168.59.103:2376
$ export DOCKER_CERT_PATH=/Users/kenzo/.boot2docker/certs/boot2docker-vm
$ export DOCKER_TLS_VERIFY=1
  • Docker Engine全体の環境設定確認
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ docker info

Containers: 0
Images: 0
Storage Driver: aufs
Root Dir: /mnt/sda1/var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 0
Dirperm1 Supported: true
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 4.0.5-boot2docker
Operating System: Boot2Docker 1.7.0 (TCL 6.3); master : 7960f90 - Thu Jun 18 18:31:45 UTC 2015
CPUs: 4
Total Memory: 1.955 GiB
Name: boot2docker
ID: G776:YBRC:OUGN:T7KF:TM43:6BTU:2PVW:HGWW:3CXO:YLCF:23ON:EJVE
Debug mode (server): true
File Descriptors: 9
Goroutines: 15
System Time: 2015-06-28T12:07:33.59750188Z
EventsListeners: 0
Init SHA1:
Init Path: /usr/local/bin/docker
Docker Root Dir: /mnt/sda1/var/lib/docker

注意点

docker infoで確認できる Storage Driveraufsとなっている場合

Dockerコンテナhttpdインストールができない。

利用するStorage Driverを事前に確認する必要がある。

DockerFileベストプラクティスまとめ

https://docs.docker.com/articles/dockerfile_best-practices/

Google Cloud SDKインストール For MacOSX

概要

2015/06/18(木) Google Cloud Platform Next に参加し
無料クーポンも頂き早速利用したくなり登録!

お目当はKubanetes!

Google Cloud SDKをインストールする際、
zsh利用していた為、ややてこずったのでまとめました。

手順

.htaccessでmod_rewriteを利用可能にする

概要

あまり意識せず利用しているmod_rewriteですが
httpd設定により使用できない場合があります。

以下設定手順をまとめました。

設定手順

/etc/httpd/conf/httpd.conf 修正

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
LoadModule deflate_module modules/mod_deflate.so    # ← 有効化
LoadModule rewrite_module modules/mod_rewrite.so # ← 有効化

AccessFileName .htaccess # ← 追加

DocumentRoot "/var/www/html"
<Directory "/var/www/html">
....
# Options Indexes FollowSymLinks
Options Indexes FollowSymLinks ExecCGI # ← ExecCGI追加

# AllowOverride None
AllowOverride All # ← Allに修正
....
</Directory>

##上記修正後 httpd 再起動

1
# service httpd restart

さくらVPS iptables設定 ~ある日警告文が届いた「ご利用中のサーバから、外部へ向けてUDP FloodによるDoSと思わしきトラフィックを確認いたしました。」~

iptables (FireWall) Setting

さくらレンタルサーバから以下のような警告文が届きました。

1
2
3
4
5
6
7
8
ご利用中のサーバから、外部へ向けてUDP FloodによるDoSと思わしきトラフィックを確認いたしました。

また、お客様のサーバを含めた複数のサーバにおいて同時に同じトラフィック波形のパケットを多数送信している事から、同じBot Netに属していると推測いたします。

お心当たりがない場合、サーバを第三者に不正利用されている可能性がございます。


現在、被害拡大防止の為の緊急措置として、当該サーバに対し通信停止措置を実施しております。予め、ご了承下さいますよう、お願いいたします。

要約すると

  • サーバを踏み台にして外部に攻撃しているみたい、だから対策しないと止めるよ?

とのこと。

対策としてOS再インストールした後、以下iptablesの設定をしました。


設定手順

以下root権限で実行

1
su -

接続済み通信許可

1
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

ローカルループバックアドレス許可

1
iptables -A INPUT -i lo -j ACCEPT

ICMP許可

1
iptables -A INPUT -p icmp -j ACCEPT

プライベートIPアドレス拒否

1
2
3
4
5
6
iptables -A INPUT -s 10.0.0.0/8 -j DROP
iptables -A INPUT -d 10.0.0.0/8 -j DROP
iptables -A INPUT -s 172.16.0.0/12 -j DROP
iptables -A INPUT -d 172.16.0.0/12 -j DROP
iptables -A INPUT -s 192.168.0.0/16 -j DROP
iptables -A INPUT -d 192.168.0.0/16 -j DROP

ブロードキャストアドレス拒否

1
2
iptables -A INPUT -d 0.0.0.0/8 -j DROP
iptables -A INPUT -d 255.255.255.255 -j DROP

フラグメントパケット攻撃対策

1
iptables -A INPUT -f -j DROP

ステルススキャン禁止

1
iptables -A INPUT -p tcp -m state --state NEW ! --syn -j DROP

IDENT port probe対策

1
iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset

PING Flood 対策

1
iptables -A INPUT -p icmp --icmp-type echo-request -m hashlimit --hashlimit 1/s --hashlimit-burst 5 --hashlimit-mode srcip --hashlimit-name input_icmp  --hashlimit-htable-expire 300000 -j DROP

以下一般的なポート許可

  • 不要な場合は設定しないで良いです。
  • ポートを変えている場合はそのポートで許可してください。

SSH ポート許可 (22)

1
iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT

HTTP ポート許可 (80)

1
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT

デフォルトポリシーの設定

1
2
3
iptables -P INPUT   DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

設定確認

1
iptables -L --line-numbers -n

設定保存 反映

1
2
service iptables save
service iptables restart

まとめ

現状この設定をしてからは特に被害にはあっていないです。

友人のさくらVPSでも同様の攻撃を受けたので教えてあげたら被害はなくなったとのことで

一定の効果はあるかと存じます。

##コマンドまとめ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p icmp -j ACCEPT
iptables -A INPUT -s 10.0.0.0/8 -j DROP
iptables -A INPUT -d 10.0.0.0/8 -j DROP
iptables -A INPUT -s 172.16.0.0/12 -j DROP
iptables -A INPUT -d 172.16.0.0/12 -j DROP
iptables -A INPUT -s 192.168.0.0/16 -j DROP
iptables -A INPUT -d 192.168.0.0/16 -j DROP
iptables -A INPUT -d 0.0.0.0/8 -j DROP
iptables -A INPUT -d 255.255.255.255 -j DROP
iptables -A INPUT -f -j DROP
iptables -A INPUT -p tcp -m state --state NEW ! --syn -j DROP
iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset
iptables -A INPUT -p icmp --icmp-type echo-request -m hashlimit --hashlimit 1/s --hashlimit-burst 5 --hashlimit-mode srcip --hashlimit-name input_icmp --hashlimit-htable-expire 300000 -j DROP
1
2
3
4
5
6
iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT

iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
1
2
service iptables save
service iptables restart

コマンドラインでURLの存在チェック

以下コマンドをMacローカルでterminalから実行

1
curl -v <URL>  2>&1 1>/dev/null | awk '{if($2~"HTTP") print}'

OK Pattern.

1
< HTTP/1.1 200 OK

NG Pattern

1
< HTTP/1.1 404 Not Found

CentOSにmysqldiffインストールし実行確認

rootユーザに切り替え

1
$ sudo su -

mysqldiff インストール

1
2
3
# cd /usr/local/src
# wget http://search.cpan.org/CPAN/authors/id/A/AS/ASPIERS/MySQL-Diff-0.43.tar.gz
# tar zxvf MySQL-Diff-0.43.tar.gz

mysqldiffにlibパスを設定

1
# vi MySQL-Diff-0.43/bin/mysqldiff
1
2
3
4
5
6
#!/usr/bin/perl -w

use lib '/usr/local/src/MySQL-Diff-0.43/lib'; ←追加

=head1 NAME

Slurpインストール

1
# yum -y install perl-File-Slurp

シンボリックリンク

1
# ln -s /usr/local/src/MySQL-Diff-0.43/bin/mysqldiff /usr/local/bin/mysqldiff

mysqldiff実行確認

1
$ mysqldiff

db1, db2 を比較

1
$ mysqldiff db1 db2