Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Thursday, April 28, 2022

DEVSECOPS: Javascript Strict Mode

 



This is the code used for the capstone project.



In the source code above, there is 'use script'

so use script is introduced into JS world with the release of ECMAscript5.

It tells the browser to change the way it executes the javascript which means it make several changes to normal javascript semantics.

It is for a better and more secured code (developers will make less mistakes).

'use script' does not apply to block statements that are enclosed in curly brackets (like if else statements).


strict mode blocks from adding properties to the global scope.

strict mode also blocks from using the name of a read-only property from the global scope (var NaN, NaN is read-only property).

strict mode also blocks from using duplicate parameter names.

strict mode also blocks from using octal numerical literals (let oct = 051 #error but answer is 41 BUT if you use let oct = 0o051 - it can show the answer on the browser - because it follows the ES6).

strict mode also blocks from creating variables in the scope from which it was called such as eval (eval ("var x = 9.8");) this is for security reasons as well. 

Friday, February 11, 2022

Node.js in GitLab

 

Step0:-

> Install Nodejs on Windows

            https://nodejs.org/en/download/

> Install Nodejs on ubuntu 

            curl -fsSL https://deb.nodesource.com/setup_17.x | bash -

            apt-get install -y nodejs


Step 1:- Install Visual Studio Code on your system.

Step2:- Create a folder say C:\nodeapp and open it in visual studio code

           cd c:\nodeapp

           code .

Step3:- Create a index.js file and write below code in this file

const express = require('express')
		const app = express()
		const port = 8080
		app.get('/', (req, res) => {
		  res.send('This is my first Node application!')
		})
		app.listen(port, () => {
		  console.log(`Application is listening at http://localhost:${port}`)
		})

Step 4:- Create package.json file and write below content

{

  "dependencies": {

      "express": "^4.16.1"

  }

 }


Step5: Run below commands in the terminal

          npm install

          node index.js


Then in GitLab:

upload index.js and package.json into the repository.

edit .gitlab-ci.yml file with these examples:


Example 1: This will result in error:

    stages:

  - build
  - deploy
build:
    stage: build
    script: 
        - apt update
        - apt install npm -y
        - npm install
deploy:
    stage: deploy
    script:
        - apt update -y
        - apt install nodejs -y
        - node index.js      

it will result in error because it didn't find the express edition.
because what is installed in build is not passed down to deploy.



Example2: to remedy the above situation, artifacts need to be added into the line of codes. The artifacts line means it will pass down the paths from build to deploy so when deploy job runs, it will have the resources with it. BUT this will result in endless loop..the job will keep on running.

stages:
  - build
  - deploy
build:
    stage: build
    script: 
        - apt update
        - apt install npm -y
        - npm install
    artifacts:
      paths:
      - node_modules
      - package-lock.json
      # expire_in: 1 week 
deploy:
    stage: deploy
    script:
        - apt update -y
        - apt install nodejs -y
        - node index.js        


Example3: to make the above to run in the background. And the pipeline is passed.

stages:
  - build
  - deploy
build:
    stage: build
    script: 
        - apt update
        - apt install npm -y
        - npm install
    artifacts:
      paths:
      - node_modules
      - package-lock.json
      # expire_in: 1 week 
deploy:
    stage: deploy
    script:
        - apt update -y
        - apt install nodejs -y
        - node index.js > /dev/null 2>&1 & 
(The ampersand is the solution, it makes the program run in background)

Example 4: Using docker image rather than scripting apt install.

stages:
  - build
  - deploy
build:
  image: node
  stage: build
  script:
    # - apt update -y
    # - apt install npm -y
    - npm install
  artifacts:
    paths:
      - node_modules
      - package-lock.json
    # expire_in: 1 week 
deploy:
  image: node
  stage: deploy
  script:
    # - apt update -y
    # - apt install nodejs -y
    - node index.js > /dev/null 2>&1 &

Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...