Golang Cron Jobs

cron in golang

在golang中,有个包github.com/robfig/cron/v3可以实现linux中类似的cron功能,使用也比较简单。 官方文档中比较详细,这里只是简单记录一下。

  c := cron.New()
	schlTime := "0 2 * * ?"
	// send reminder for org members
	c.AddFunc(schlTime, func() {
					log.Info("Send reminder for org member expiration job started")
					defer func() {
									log.Info("Send reminder for org member expiration job finished")
					}()

					log.Info("started to remind expired org members")
					err = task.RemindExpiredOrgMember(14)
					if err != nil {
									log.Errorf("\nerror: %v\n", err)
					}
					log.Info("end to remind expired org members")
	})
  c.Start()
	defer c.Stop()
……

Continue reading

Gin Jwt Middleware

JWT in golang

golang中的jwt包一般是使用 github.com/dgrijalva/jwt-go, 而且godoc中已经有些example,这里只是记录一下使用 jwt.StandardClaims的情况。

func parseToken(tokenString string) (*jwt.StandardClaims, error) {
        token, err := jwt.ParseWithClaims(tokenString, &jwt.StandardClaims{}, func(t *jwt.Token) (interface{}, error) {
                return jwtKey, nil
        })

        if err != nil {
                log.Errorf("error: %v", err)
                return nil, fmt.Errorf("failed to parse the token string: %s", tokenString)
        }

        log.Printf("claims: %v", token.Claims)

        if claims, ok := token.Claims.(*jwt.StandardClaims); ok && token.Valid {
                return claims, nil
        } else {
                return nil, errors.Wrap(fmt.Errorf("failed to valiate the token"), "failed")
        }
}

这个方法会把一个string通过jwt的parsewithclaims恢复成StandardClaims,如果有错误会返回相应的error,这样的话,在middleware中可以 在req header中取到jwt然后尝试parse一下从而知道请求中的是valid的。

……

Continue reading

Liberty Profile Tls V1 Gaps

Gap - TLS version 1.0/1.1 detected

如果在做security scan的时候,有这两个gap,需要修改两个地方

  • liberty profile server.xml

add sslProtocol="TLSv1.2" to ssl

<ssl id="defaultSSLConfig"
	sslProtocol="TLSv1.2"
     keyStoreRef="defaultKeyStore"
     securityLevel="CUSTOM" enabledCiphers="TLS_RSA_WITH_AES_128_CBC_SHA TLS_RSA_WITH_AES_128_CBC_SHA256 "/>
  • server.env

add

JVM_ARGS=-Dhttps.protocols=TLSv1.2
……

Continue reading

React for Beginners

Prerequesite

  • node
  • javascript
  • html
  • css
  • es6
  • code editor

create-react-app

npm install -g create-react-app
create-react-app app-name

React component

state

class component里面管理状态使用state

class Counter extends Component {
  state = {
    counter: 10,
    title: "this is demo",
    products: [
      {
        id: 1,
        name: "ok",
      },
      {
        id: 2,
        name: "bad",
      },
    ],
  };
  // ..... other
}

render

jsx

expression

attributes

list/array

conditional rendering

handle events

binding event handlers

updating state

event arguments

passing data to component

passing children

debug react app

props vs state

raising and handle events

multiple component in sync

lifting state up

functional component

destructure arguments

lifecycle hooks

……

Continue reading

添加第三方jar到Spring Boot application

背景

有时候需要添加第三方的jar包到spring boot工程中,因为不能从maven repository直接下载,所以需要包含在代码库中。 一般需要在

src/main

目录下,可以建个新目录 lib, 然后把jar文件放到这个目录下,也就是 src/main/lib下.

然后在pom.xml中需要添加

  • groupId/artifactId/version/scope都需要填上
        <dependency>
            <groupId>com.ibm.bluepages</groupId>
            <artifactId>bpjtk</artifactId>
            <version>3.0.6.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/lib/bpjtk-v3.0.6.0.jar</systemPath>
        </dependency>
  • 在 spring-boot-maven-plugin中加上 includeSystemScope true
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                  <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
……

Continue reading

Postgres 备份与恢复

背景

有时候我们需要备份 schema或者包括数据,然后恢复到另外一个postgres db中。

备份

可以使用 pg_dump 来备份,或者加上 –schema-only 只备份schema.

pg_dump --file "/root/repo/uat-copy.tar" --host "db host name" --port "23123" --username "admin" --password --verbose --format=t --blobs --schema-only "compose"

恢复

同样的,可以使用pg_restore来恢复

pg_restore --host "hostname here" --port "31090" --username "usernamehere" --password --dbname "ibmclouddb" --verbose "/root/repo/uat-copy.tar"

不管备份还是恢复,都会要求输入密码。

……

Continue reading

Auto Login Server Without Password

配置方法,做个小记录

server 上要配置允许

用户电脑

在 .ssh/config 文件中

Host *
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa

Host xxx
  HostName xx.xx.xx.xx
  User jerry

把pub key传到server

ssh-copy-id -i ~/.ssh/id_rsa.pub xx@xx.xx.xx.x

可以了

……

Continue reading

Vue File Download From Spring Boot

File download

如果spring boot的写法是我在另外一个文章, 返回的response其实是一个Blob, 这一点要记住

          this.axios.get(url, { responseType: 'blob', timeout: 0 }).then(response => {
              // response is Blob <-------
                const url = window.URL.createObjectURL(response)
                const a = document.createElement('a')
                a.style.display = 'none'
                a.href = url
                a.setAttribute('download','file.xlsx')
                document.body.appendChild(a)
                a.click()
                document.body.removeChild(a);
                window.URL.revokeObjectURL(url);
          });
……

Continue reading

Spring Boot File Download

spring boot application

提供下载的代码,可以返回一个Resource

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class ReportController {



    @GetMapping(value = "/filedownload")
    public ResponseEntity<InputStreamResource> downloadFile(HttpServletRequest request) {

        // .......................
        // .......................
        // .......................

        ByteArrayInputStream stream = xxxxxx;// get the stream

        return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.xlsx")
                .body(new InputStreamResource(stream));

    }

}
……

Continue reading

Azure AD SAML SSO with Node Application

Azure AD saml sso

代码

主要是参考文档

  • 安装 passport-saml
npm install passport-saml
  • use passport
passport.use(new SamlStrategy(
  {
    path: '/account/auth/blueid/callback', // 这个是callback需要稍后实现
    entryPoint: '', // 这个是azure AD的一个配置, saml-based sso login url
    issuer: 'cobeedev', // basic saml configuration -> Identifier (Entity ID)
    cert: '', // refer later
    signatureAlgorithm: 'sha256'
  },
  async function(profile, done) {
  // in this function, you can process profile to get necessary information
    console.log(profile);
    // lazy require
    const usersManagement = require('../../controllers/users/usersManagement');
    const ssoUser = jsonUtil.profile2User(profile);
    let dbUser = await usersManagement.getUserByAzureMail(ssoUser.email);
    // if user was not found in db
    // process the scenario
    if (!dbUser) {
      await usersManagement.processAzureSSOUser(ssoUser);
      dbUser = await usersManagement.getUserByAzureMail(ssoUser.email);
    }
    return done(null,{
      ...dbUser,
      azureUser: ssoUser
    });
  }
));

	app.use(passport.initialize());
	app.use(passport.session());
  • 实现 callback get request to saml login page, and post request to handle callback
router.get('/blueid', passport.authorize('saml', { }));
router.post('/blueid/callback', function (req, res, next) {
  console.log('in callback');
  var redirect_url = '/account/getprojects';
  if(req.session.originalUrl){
    let url=req.session.originalUrl;
    let ida=url.indexOf('url=')
    if(ida!=-1){
      redirect_url+="?"+url.slice(ida,url.length)
      req.session.originalUrl=redirect_url
    }
  }
  passport.authenticate('saml', {
    successRedirect: redirect_url,
    failureRedirect: '/#/account/notregistered'
  })(req, res, next);
});
  • about the cert, refer to doc
……

Continue reading