2014년 12월 17일 수요일

Scalable Distributed Erlang

Scalable-Distributed Erlang에 대한 프리젠테이션입니다.

http://www.erlang-factory.com/upload/presentations/659/FactoryLite-SDErlangDesign.pdf

가장 인상 깊은 말은,
"All to all connections are not scalable onto
1000s of nodes."입니다.

1000개의 노드끼리 n-to-n 통신을 하면, 1백만개의 커넥션이 생기지요.
이 문제를 해결 할 수 있을지...

저는 차라리 reliable UDP로 통신하는 것이 어떨까 생각해 봅니다.

2014년 12월 2일 화요일

Google Storage API in Erlang


Erlang용 Native SDK가 없기 때문에,

직접 rest api를 만들어서 OAuth2에서 token을 받고, 
(  https://developers.google.com/accounts/docs/OAuth2ServiceAccount  )
그 token으로 API를 호출해야 한다. 


이 과정에서 시행착오가 있을 수 있는 부분이 몇가지 있다.

developer console에서 받은 p12 파일을 먼저 컨버팅해야 한다.


openssl pkcs12 -in some.p12 -out some.crt.pem -clcerts -nokeys

openssl pkcs12 -in some.p12 -out some.key.pem -nocerts -nodes

Certificate과 Key가 함께 있기 때문에, 일단 분리하고,


openssl rsa -in some.key.pem -out some.key2.pem


some.key2.pem이 RSA Private Key가 담긴 파일이 된다. 아래 코드는 이 파일을 RSA Private Key로 변환하는 코드이다.

get_private_key() ->
    FileName = m_config:get(gce_key_pem_file,"priv/google/some.key2.pem"),
    { ok , F } = file:read_file(FileName),
    PrivateKeyEntry = public_key:pem_decode(F),
    PrivateKeyEntry1=hd(PrivateKeyEntry),
    PrivateKey = public_key:pem_entry_decode(PrivateKeyEntry1),
    #'RSAPrivateKey'{publicExponent=Exponent
                    ,modulus=Modulus
                    ,privateExponent=PrivateExponent} = PrivateKey,
    [Exponent, Modulus, PrivateExponent].



그리고, JWT header, JWT Claim Set을 적절히 잘 만들고, 요 키로 sign을 해야 한다.




ToEncrypt = << (jwt_header())/binary , $. , (jwt_claim_set())/binary  >>,
S=base64url:encode(crypto:sign(rsa,'sha256',ToEncrypt,get_private_key())),
<< <<"grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=">>/binary , ToEncrypt/binary , $. , S/binary >>. 


완성된 full source

m_oauth2.erl
https://gist.github.com/wdshin/2eb6998913b3b95454bc

m_google_storage.erl
https://gist.github.com/wdshin/54896699bf1f5aed926e


2014년 11월 19일 수요일

First Experience on Google Compute Engine

Google Compute Engine에서 Erlang,Riak,Cowboy 기반 서비스 설치 완료했습니다.

인상적인 것은, 다른 Region에서도 VM Instance를 만들면, 프로젝트가 쓰고 있는 같은 Privte 망에 붙어서 생성됩니다. 
그래서, cookie 만 같으면, Region간 통신이 바로 됩니다.

Regioin간 통신은 150ms 정도 나오고요.
같은 Region내의 다른 Zone간에는 1ms 이하로 떨어집니다.


2014년 11월 5일 수요일

Diffie Hellman Key Exchange in Erlang


test_dh() ->

    DHParams = crypto:dh_generate_parameters(1024,2),
    { PubKey1 , PriKey1 } = crypto:generate_key(dh,DHParams),
    { PubKey2 , PriKey2 } = crypto:generate_key(dh,DHParams),
 
    SharedKey1 = crypto:compute_key(dh,PubKey2,PriKey1,DHParams),
    SharedKey2 = crypto:compute_key(dh,PubKey1,PriKey2,DHParams),
 
    SharedKey1 =:= SharedKey2.