介绍

OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用

认证过程

  • 使用OAuth进行认证和授权的过程如下所示:
  • 用户访问客户端的网站,想操作自己存放在服务提供方的资源。
  • 客户端向服务提供方请求一个临时令牌。
  • 服务提供方验证客户端的身份后,授予一个临时令牌。
  • 客户端获得临时令牌后,将用户引导至服务提供方的授权页面请求用户授权。在这个过程中将临时令牌和客户端- 的回调连接发送给服务提供方。
  • 用户在服务提供方的网页上输入用户名和密码,然后授权该客户端访问所请求的资源。
  • 授权成功后,服务提供方引导用户返回客户端的网页。
  • 客户端根据临时令牌从服务提供方那里获取访问令牌 。
  • 服务提供方根据临时令牌和用户的授权情况授予客户端访问令牌。
  • 客户端使用获取的访问令牌访问存放在服务提供方上的受保护的资源

oauth2

oauth2

Flow

六种流程:

  • User-Agent Flow 客户端运行于用户代理内(典型如web浏览器)。
  • Web Server Flow 客户端是web服务器程序的一部分,通过http request接入,这是OAuth 1.0提供的流程的简化版本。
  • Device Flow 适用于客户端在受限设备上执行操作,但是终端\a户单独接入另一台电脑或者设备的浏览器
  • Username and Password Flow 这个流程的应用场景是,用户信任客户端处理身份凭据,但是仍然不希望客户端储存他们的用户名和密码,这个流程仅在用户高度信任客户端时才适用。
  • Client Credentials Flow 客户端适用它的身份凭据去获取access token,这个流程支持2-legged OAuth的场景。
  • Assertion Flow 客户端用assertion去换取access token,比如SAML assertion。

其他网站例子:

  • 豆瓣 User-Agent、Web Server、Native Application Username and Password FlowWiki
  • 淘宝 User-Agent、Web Server、Native Application、登陆账号退出流 Username and Password FlowWiki
  • Sina User-Agent(站内应用)、Web Server、Native Application Username and Password FlowWiki

选择:

  • Username and Password Flow(适合官方第一应用)
  • Web Server Flow(适合第三接入,比较常用)

Draft

Ruby Provider 实现

Client Credential Flow:

  • 过程

oauth2

  • 请求参数:

    client_id: 必选参数,应用的唯一标识

    client_secret: 必选参数,应用的唯一标识

    user_name: 用户账号

    user_password: 用户密码

    grant_type: password

  • 例子:

http://server/oauth/token?client_id&client_secret&user_name&user_password 

method: post
  • 返回参数:
{ 		
"access_token":"de6780bc506a0446309bd9362820ba8aed28aa506c71eedbe1c5c4f9dd350e54",
"token_type": "bearer", 
"expires_in": 7200,
"refresh_token":"8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1"
}

access_token不再长期有效。在授权获取access_token时会一并返回其有效期,也就是返回值中的expires_in参数。

在access_token使用过程中,如果服务器返回错误:“access_token_has_expired ”,此时,说明access_token已经过期,就需要发送refresh_token的方式来换取新的access_token和refresh_token。

  • 请求参数

    client_id: 必选参数,应用的唯一标识

    client_secret: 必选参数,应用的唯一标识

    grant_type: refresh_token

    refresh_token: refresh_token

  • 例子

    https://server/auth2/token?client_id=0b5405e19c58e4cc21fc11a4d50aae64&client_secret=edfc4e395ef93375&redirect_uri=https://www.example.com/back&grant_type=refresh_token&refresh_token=5d633d136b6d56a41829b73a424803ec
    
    method: post
  • 返回参数
    {
    "access_token":"0e63c03dfb66c4172b2b40b9f2344c45",
    "expires_in":3920,
    "refresh_token":"84406d40cc58e0ae8cc147c2650aa20a",
    }

Rails gem:

  • https://github.com/applicake/doorkeeper
  • https://github.com/nov/rack-oauth2
  • https://github.com/opro/opro
  • https://github.com/songkick/oauth2-provider
  • https://github.com/freerange/oauth2-provider
  • https://github.com/socialcast/devise_oauth2_providable
  • https://github.com/intridea/oauth2

Rails Logger


介绍:

本文并不是介绍Rails Logger是怎样工作,仅仅从一个现实的例子来说明应该怎样使用Rails Logger

目的:

或许你会在一个rails app里面有许多不同apps,这个时候,你会想把它们的log在不同的log file上,而不是全部堆积在一起;又或许,你会想把里面一个单独功能log给分离出来

How to:

进入项目 config/environments/ ,分别在development.rb staging.rb production.rb配置
1
config.logger = Logger.new("the_log_path_you_want_log") 

另外一种是指定文件与输入

1
2
3
new_logger = Logger.new("#{Rails.root}/log/#{the_file_log_name}")

new_logger.info "new log messages here"

好了,上面这些就搞定了log自定义

Munin alert

Munin alert支持几种方式

Email alert:

但是这种在我们天朝下面是基本不能用的,所以需要自己去写脚本发送email

Syslog Alert:

To send syslog message with priority use a command such as this:

1
contact.syslog.command logger -p user.crit -t "Munin-Alert"

Alerts to or through external scripts:

To run a script (in this example, ‘script’) from Munin use a command such as this in your munin.conf. Make sure that:

  1. There is NO space between the ‘>’ and the first ‘script’

  2. ‘script’ is listed twice and

  3. The munin user can find the script – by either using an absolute path or putting the script somewhere on the PATH – and has permission to execute the script.

Code:

1
contact.person.command >script script
This syntax also will work (this time, it doesn’t matter if there is a space between ‘ ’ and the first ‘script’ … otherwise, all the above recommendations apply):
1
contact.person.command | script script

我们上面所说的自己写脚本发送email就是通过这种方式详细可以参考我写的一个脚本

Munin-Alert-Email

Test & Debug

根据上面Munin-Alert-Email README 使用脚本,然后接下来就是测试和Debug,Munin在跑munin-plugins时候,用的是 root用户,而在执行alert时候,是munin用户,这样,你就需要保证你执行脚本在munin下是可以执行的

  • /var/log/munin/munin-limits.log: 查看每次limit log
  • /var/lib/munin/limits:每次limits输出
  • /usr/share/munin/munin-limits –debug: debug limits,如果邮件无法发送,你可以尝试下这个debug,如果出现lock,这个时候你就需要把对应在跑的 munin-limits 给 kill 掉

最后,就等待5分钟或者Debug limit之后,就可以接受到邮件

Munin

###目的

对于Linux的系统来说,日志文件是很重要的,很多异常与记录都可以通过log来查看,但是日夜积累,这样log文件是非常大的;Logrotate,是一个日志管理工具,用于分割日志文件,删除旧的日志文件,并创建新的日志文件,可以很大的节省硬盘空间

###安装

一般来说, Linux都是默认安装好了

###结构

  • /etc/logrotate.conf #logrotate 配置文件
  • /etc/logrotate.d/ #存放其他配置文件

###commnad

logrotate命令格式:

1
2
3
4
5
6
	logrotate [OPTION...] <configfile>
	-d, --debug :debug模式,测试配置文件是否有错误。
	-f, --force :强制转储文件。
	-m, --mail=command :发送日志到指定邮箱。
	-s, --state=statefile :使用指定的状态文件。
	-v, --verbose :显示转储过程。

###配置

参数说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
	compress 通过gzip 压缩转储以后的日志 
	nocompress 不需要压缩时,用这个参数 
	copytruncate 用于还在打开中的日志文件,把当前日志备份并截断 
	nocopytruncate 备份日志文件但是不截断 
	create mode owner group 转储文件,使用指定的文件模式创建新的日志文件 
	nocreate 不建立新的日志文件 
	delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩 
	nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。 
	errors address 专储时的错误信息发送到指定的Email 地址 
	ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。 
	notifempty 如果是空文件的话,不转储 
	mail address 把转储的日志文件发送到指定的E-mail 地址 
	nomail 转储时不发送日志文件 
	olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统 
	noolddir 转储后的日志文件和当前日志文件放在同一个目录下 
	prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行 
	postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行 
	daily 指定转储周期为每天 
	weekly 指定转储周期为每周 
	monthly 指定转储周期为每月 
	rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份 
	tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-		orig, .rpmsave, v, 和 ~ 
	size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).

根据上面配置,在/etc/logrotate.d/ 创建你需要的配置文件,下面以Rails应用为例子

1
	touch /etc/logrotate.d/rails-app

配置文件

1
2
3
4
5
6
7
	#{rails_path}/*.log {
		notifempty
		missingok
		daily
		rotate 5
		compress
	}

然后测试执行

1
	logrotate -d /etc/logrotate.d/rails-app

或者全部执行:

1
	/usr/sbin/logrotate -vf /etc/logrotate.conf

没有错误的话,就表示已经配置成功了,可以去对应的logw目录下面看看

###Logrotate如何自动执行

在/etc/cron.daily目录下有logrotate执行的脚本, 通过crontab程序每天执行一次。

A networked resource monitoring tool Munin

Server

install munin master:

1
sudo apt-get install munin

munin floders:

1
2
3
4
5
6
7
8
9
10
11
- /etc/munin 配置文件

- /etc/munin/munin.conf 主服务配置文件

- /etc/munin/munin-node.conf 节点配置文件

- /etc/munin/plugins 插件存放

- /var/lib/munin 统计数据文件

- /var/cache/munin/www Htmldir 输出文件

munin master主要有两个命令:

1
2
3
- munin-cron #定时任务

- munin-run  #执行plugins

网页监控:

这个监控就需要一个 web servie 帮忙, 安装 apache:

1
sudo apt-get install apache2

other web server configurations

配置 apache:

1
  cd /etc/apache2/sites-available/default
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
<VirtualHost *:80>

	ServerAdmin webmaster@localhost

	ServerName   munin.example.com

	DocumentRoot /var/cache/munin/www/

	<Directory />
   
   	Options FollowSymLinks
   
   	AllowOverride None

	</Directory>

	LogLevel notice

	CustomLog /var/log/apache2/munin.access.log combined

	ErrorLog /var/log/apache2/munin.error.log

	ServerSignature On

</VirtualHost>

打开localhost就可以看看效果

配置 munin master,在 /etc/munin.conf 配置发送信息节点主机

常用配置:

1
2
3
4
5
6
7
  [hostname.example.com]  
    address 10.0.0.1  
    use_node_name yes 
  
  [hostname2.example.com]  
    address 10.0.0.2  
    use_node_name yes

使用ssh配置 munin-async

Node

安装 munin node:

1
sudo apt-get install munin-node

配置 /etc/munin/munin-node.conf, 通过allow来设置哪些主机可以访问:

1
allow ^127\.0\.0\.1$

重启 munin-node,记得 node 服务器要开启4949端口:

1
sudo service munin-node restart

Test and Debug

在 munin master 机器上,看看是否连接成功:

1
telnet xxx.xxx.xxx.xx[ip] 4949

root,tailf log:

1
2
3
4
5
sudo su

tailf /var/log/munin/munin-node.log # 节点 log

tailf /var/log/munin/munin-update.log  # 页面更新log

Common commands

1
2
3
4
5
6
7
sudo munin-node-configure --shell #查看有哪些plugins

sudo su #切换到root用户

su - munin --shell=/bin/bash #切换到munin

munin-cron #执行generate html

其他功能待续…