Sunday, 11 May 2025

Edit host file for Window 11 WSL (Windows Subsystem for Linux)

You need to edit the following file using notepad and run as admin

 C:\Windows\System32\drivers\etc\hosts

If you edit subsystem linux's /etc/hosts, it will not affect browser such as Google Chrome

Access two github accounts in one linux machine

Assume you already can access your person github repositories using public key id_rsa.pub

  1. Create another ssh private / public key pair
    # file name: ~/.ssh/id_rsa2
     ssh-keygen
  2. upload public key to your company's github
  3. create a ssh config at .ssh/config
    Host github.com
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa
      AddKeysToAgent yes
      ServerAliveInterval 60
      ServerAliveCountMax 30
    
    # Work GitHub account
    Host github-work
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa2
      AddKeysToAgent yes
      ServerAliveInterval 60
      ServerAliveCountMax 30
  4. clone work repository
    git clone git@github-work:mycompany/jackt.git
  5. clone your person repository as normal because it will use the default ssh config

Tuesday, 15 April 2025

Access Mysql Database hosted on AWS EC2

To access the database, need to set up two things.

Step 1: add rule to the security group attached to the instance

  1. Edit inbound rule of the security group
  2. add a rule. Type: MYSQL/Aurora Soure: custom such as 137.229.127.0/24

Step 2: update privilidge of Mysql database

  1. log into the database as root or admin
  2. #works for mysql 5.7.8 and up. To change a user's ip
      RENAME USER 'teau'@'51.27.292.63' TO 'teau'@'125.256.111.%';
      # for new user, need to grant permission
  3. FLUSH PRIVILEGES;

Trouble shooting

If do not do the step one, most likely will see a connection timeout

If do not do the second step, will see something like

[MySQL][ODBC 8.0(w) Driver]Host '*.*.***.**' is not allowed to connect to this MySQL server
Unable to connect to the MySQL server "***.***.**.**". Check that the server is running and that you have
access privileges to the requested database.

Some useful Mysql commands

select version();

select user, host from mysql.user;

SHOW GRANTS FOR 'leo'@'52.30.111.16';

Tuesday, 25 March 2025

Schedule post to an end point using Amazon EventBridge Rules

  1. set up a SNS topic
  2. Create a subscription for this SNS topic. Choose protocol https and enter end point such as
    https://paysomething.sandbox.mysite.io/v1/tasks/process
  3. Go to Amazon EventBridge to create a rule
  4. Make the above SNS topic as target of the rule, and type is Schedule

Friday, 14 March 2025

AWS WAF

Bock ips to elb

    • Click IP set tab
    • Click Create IP set button
    • Given name, description and IP addresses
    • Click Create IP set button to save the IP set
    • Click Web ACLs tab
    • Click Create Web ACL button
    • Follow steps to create web ACL. Add the above ip set rule and action is block
    • For Default web ACL action for requests that don't match any rules, choose allow
    • Click Web ACLs tab agin. The newly created ACL should show up in the list
    • Click that ACL
    • Click Associate AWS resources tab
    • Click Add AWS resource button
    • Select the ebl and assoicate it to this Web ACL
  1. Test it using your own ip
  2. Go back to that Web ACL home page to check traffic log

Tuesday, 4 March 2025

Add Custom Http Header

Using curl


curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'cookiename: blue',
    'X-Apple-Store-Front: 143444,12'
]);

Google Chrome

Use ModHeader extension

Postman

Click Headers tab to add custom header

Thursday, 30 January 2025

PHP Propel

fake option for migration

docker-compose run --rm -w /var/src/Propel api ./../vendor/bin/propel migrate --fake

it doesn't actually run the migrations, but it marks them as having been completed. It will add versions in propel_migration table.

Friday, 24 January 2025

Public key and private key

Mainly there are two use cases.

  • encrypte message using public key. Then decrypt message using private key
  • digitally sign message using private key. Then verify signature using public key

AWS policy to restrict ips to AWS Gateway API

Here is a sample policy. Only ips in the list will allow to call that API

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": [
                        "223.333.923.28/32",
                        "162.222.229.139/32"
                    ]
                }
            },
            "Action": [
                "execute-api:Invoke"
            ],
            "Resource": [
                "arn:aws:execute-api:us-west-1:112334444444:1wertttt/*/POST/v1/bill",
                "arn:aws:execute-api:us-west-1:112334444444:1wertttt/*/GET/vi/bill/*"
            ],
            "Effect": "Allow"
        }
    ]
}

Monday, 13 January 2025

docker container run breakdown

Here is a command to run php 8 in a host machine which only has php 7 installed

docker run --rm --volume `pwd`:/var/src/ -w /var/src php:8.0.26-cli-alpine3.16  php ./bin/swagger_gen.php ./rest/swagger/v1/

Breakdown

  • --rm Automatically remove the container and its associated anonymous volumes when it exits
  • --volume `pwd`:/var/src/ Bind mount a volume (docker run --volume host-path:container-path
  • pwd present working directory of your host machine
  • -w Working directory inside the container

Saturday, 11 January 2025

Indirect modification of overloaded property has no effect

<?php
error_reporting(E_ALL);

class PropertyTest
{
    /**  Location for overloaded data.  */
    private $data = array();


    public function __set($name, $value)
    {
        $this->data[$name] = $value;
    }

    public function __get($name)
    {
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        } else {
            throw new Exception($name . " not found");
        }
    }
}

$obj = new PropertyTest();
$obj->goods = ["a", "b"];

//the line below will throw notice
//Indirect modification of overloaded property PropertyTest::$goods has no effect
$obj->goods[] = "c";

//still got ["a", "b"]
var_dump($obj->goods);

To fix the error, replace the last two lines of codes with the below.

$obj->goods= array_merge($obj->goods, ['c']);

//got ["a", "b", "c"]
var_dump($obj->goods);

Here is another example

$obj = new PropertyTest();
$obj->test=["a"=>2];
var_dump($obj->test);
//will throw the notice and will not change value
$obj->test['a'] =3;
//the result is the same as what we get from the last dump
var_dump($obj->test);

To make it work as expected

$obj = new PropertyTest();
$obj->test=["a"=>2];
var_dump($obj->test);

$ans = $obj->test;
$ans['a'] = 3;
$obj->test = $ans;
var_dump($obj->test)