Tuesday, November 22, 2022

Istio service mesh

An Istio service mesh is logically split into a data plane and a control plane.

  • The data plane is composed of a set of intelligent proxies (Envoy) deployed as sidecars. These proxies mediate and control all network communication between microservices. They also collect and report telemetry on all mesh traffic.
  • The control plane manages and configures the proxies to route traffic.

 In Istio, configure a single service mesh to span any number of clusters, or can configure a single mesh to include multiple clusters 

     

Envoy Proxy:

Processes the inbound/outbound traffic from inter-service and service-to-external-service transparently. Istio components that interact with data plane traffic. Envoy proxies are deployed as sidecars to services. It will have Service entry (fully qualified name)

 

Some of the Istio features and tasks enabled by Envoy proxies include:

  • Traffic control features: enforce fine-grained traffic control with rich routing rules for HTTP, gRPC, WebSocket, and TCP traffic.
  • Network resiliency features: setup retries, failovers, circuit breakers, and fault injection.
  • Security and authentication features: enforce security policies and enforce access control and rate limiting defined through the configuration API.
  • Pluggable extensions model based on Web-Assembly that allows for custom policy enforcement and telemetry generation for mesh traffic

Istiod:

Istiod provides service discovery, configuration and certificate management. Istiod converts high level routing rules that control traffic behavior into Envoy-specific configurations, and propagates them to the sidecars at runtime

  • Pilot: Pilot provides service discovery for the Envoy sidecars, traffic management capabilities for intelligent routing (e.g., A/B tests, canary deployments, etc.), and resiliency (timeouts, retries, circuit breakers, etc.)

  • Citadel: Citadel enables strong service-to-service and end-user authentication with built-in identity and credential management. It provides encryption of traffic between Envoy Sidecar Proxies using mutual TLS
  • Galley: Galley is Istio’s configuration validation, ingestion, processing and distribution component. It is responsible for insulating the rest of the Istio components from the details of obtaining user configuration from the underlying platform (e.g. Kubernetes).

Virtual services route traffic to a given destination and use destination rules to configure what happens to traffic for that destination.

Ingress gateway configuration lets HTTP traffic from Istio ingress gateway into the mesh on port 80. To specify routing and for the gateway to work as intended, we also bind the gateway to a virtual service. 

setup Istio service mesh in AWS EKS

Prerequisites: 

·       kubectl v1.18.0

·       helm v3.2.1

·       Istio 1.8.2

·       terraform v0.12.30

·       aws-iam-authenticator

·       Programming access to AWS account. 

Complete view for Istio:

·       Istio Gateway - connect outside to Service Mesh clusters

·       Ingress Gateway - request coming in / request connecting to Service mesh

  •  It is Envoy proxies, running on separate POD as Istio proxy container, it runs only Istio Envoy Proxy nothing else on this container

§  Create Virtual Service
§  Create Destination Rule
§  Attach routing rules using virtual service and Destination Rule to Envoy proxies

  • Egress Gateway - Connecting from Mesh to outside
    •  It is Envoy proxies, running on separate POD as Istio proxy container, it runs only Istio Envoy Proxy nothing else on this container
    • §  Create Virtual Service
      §  Create Destination Rule
      §  Attach routing rules using virtual service and Destination Rule to Envoy proxies

·       Deployment for Ingress Gateway:

      • Running Envoy Proxies POD
    • Service for Ingress Gateway to connect to envoy Proxy

  • Service Object (Gateway Object) to enable Ingress Gateway to Receive Traffic from outside
  • Virtual Service to link routing services to routing rules to Ingress Gateway (Istio Proxy)
    • Gateways object will host details which will allow host / external world connect to the application and filters other requests.
  • Ingress Gateway Service come with Istio installation
  • There are two types of virtual service, first one at ingress gateway layer and other one is inside the service mesh.
    • Virtual Service enable further routing using "gateway", this "gateway" allow request to enter into Mesh and forward to Ingress Gateway/Proxy.
    • Other Virtual service will allow inter service communications and will configure on each POD with Istio Service Mesh


  • Launch k8s cluster in AWS EKS using terraform. deploy EKS cluster. prepare namespaces to Istio service mesh. deploy each application to own namespace.
  • Kiali is an observability console for Istio with service mesh configuration and validation capabilities
- Building an Istio service mesh for a microservices application deployed on Kubernetes


K8, AWS ALB WITH ISTIO INGRESSGATEWAY:

 

All traffic through the IngressGateway, we can route traffic differently depending on hostnames, most easily by using prefixes, by using Virtual Services.



Wednesday, September 28, 2022

Kafka Internal



Kafka Components:

 

  • Producer
    • Publish the message to a topic, 
    • messages are appended to one of the topic

  • Consumer
    • Subscriber of the messages from a topic, one or more consumer can subscriber a topic
    • Subscriber can subscribe a topic from different partition, called consumer group
    • Two consumer of the same consumer group CAN NOT subscribe the messages from the same partition

  • Message
    • Kafka message consists of an array of bytes, addition to optional metadata is called Key
    • Kafka can write the message in real time and batch mode
      • Batch mode increases the throughput but decreases the latency, hence there is a trade-off between latency and throughput.

  • Events
    • An event represents a fact that happened in the past.
    • Events are immutable and never stay in one place.
    • They always travel from one system to another system, carrying the state changes that happened.
      • Topic groups related events together and durably stores them. 

  • Streams
    • An event stream represents related events in motion.

 

  • Topic
    • topic is a logical concept in Kafka
    • Producers published the message to a topic and consumer consumes the message
    • Can be considered like a folder in a file system
    • Each topic can have multiple Partitions and message stored in Partitions
    • Topic is held by broker that stores events and managed by broker
      • a topic is a materialized event stream. In other words, a topic is a stream at rest
    • A topic is a stream of data comprising individual records
      • Example:
        • Kafka topic is a table in a database or folder in a file system 

  • Partition
    • Kafka’s topics are divided into several partitions. 
    • a partition is the smallest storage unit that holds a subset of records owned by a topic.
    • Each partition is a single log file where records/message are written to it in an append-only fashion.
 
  • Offset
    • Each record in a partition is assigned and identified by its unique offset
    • a sequence number is assigned to each message in each partition of a Kafka topic, this is called Offset
    • As soon as any message arrives in a partition, a number is assigned to that message.
    • For a given topic, different partitions have different offsets.
    • The offset number is always local to the topic partition.


  • Node
    • Kafka node host kafka servers (configure Kafka broker)
    • Kafka is a distributed, partitioned and replicated pub-sub messaging system.
    • Kafka stores messages in topics (partitioned and replicated across multiple brokers).
    • Producers send messages to topics where consumers or their consumer groups read
  • Broker
    • A broker is just an intermediate entity that exchange message between a producer and a consumer
      • For Kafka Producer,
        • it acts as a receiver,
      • and for Kafka Consumer,
        • it acts as a sender.
    • Brokers are heart of Kafka body that are also called the Kafka server.
    • Each server node in the cluster is a “broker,”
    • Handles all requests from clients (produce, consume, and metadata) and keeps data replicated within the cluster.
    •  There can be one or more brokers in a cluster.
    • A Kafka broker allows consumers to fetch messages by topic, partition and offset.
    • Kafka brokers can create a Kafka cluster by sharing information between each other directly or indirectly using Zookeepe
  • Cluster
    • Kafka cluster consists of one or more servers (Kafka brokers) running Kafka.
      • It is nothing but just a group of computers that are working for a common purpose
      • it also has a cluster having a group of servers called brokers
        • It have combination of Servers which will have brokers on it, means a server will have brokers, a multiple servers will have multiple Brokers which will called Cluster
    • Producers are processes that push records into Kafka topics within the broker.
    • A consumer pulls records off a Kafka topic.
    • Kafka cluster is a combination of multiple Kafka nodes
    • Producers send records to clusters
    • store those records and then pass them to consumers
    • Each server node in the cluster is a “broker,”
    • which stores the data provided by the producer until it is read by the consumer
    • A Kafka cluster has exactly one broker that acts as the Controller

 

  • Zookeeper
    • Management of the brokers in the cluster is performed by Zookeeper.
    • There may be multiple Zookeepers in a cluster
    • Keeps the state of the cluster (brokers, topics, users)
    • It manage and coordinate with the set of Kafka broker.
    • The Zookeeper service is mainly used to inform the Kafka producer and Kafka consumer about the presence of the number of broker in the environment or cluster.
      • It also keeps track of Kafka topics, partitions, offsets
  • Consumer Group
    • is a group of consumers
    • Multiple consumers combined to share the workload.
    • dividing a piece of large task among multiple individuals.
    • There can be multiple consumer groups subscribing to the same or different topics.
    • Two or more consumers belonging to the same consumer group do not receive the common message.
      • They always receive a different message because the offset pointer moves to the next number once the message is consumed by any of the consumers in that consumer group.
    • a single consumer read all messages from a single topic. A single consumer will read messages from a number of Kafka topics , generally a consumer will be configured in a group of consumers
    • multiple Consumer Groups reading from the same topic. Each Consumer Group will maintain its own set of offsets and receive messages independently from other consumer groups on the same topic. To say it simply, a message received by Consumer Group 1 will also be received by Consumer Group 2. Within a single consumer group, a consumer will not receive the same messages as other consumers in the same consumer group. 

  • Push
    • Consumer can be overwhelmed by flooded message by producer
    • Difficulties dealing with diverse consumers.
  • Pull
    • Consumer can pull the record as per capacity from broker
    • Batching is created for pulling the data as per consumer
    • Kafka can pull the batch on interval of time fix or the required byte size
  • Kafka services
    • Kafka Broker
    • Kafka consumer
    • Kafka Producer,
    • Zookeeper
  • Kafka Api
    • Producer API: used to publish a stream of records to a Kafka topic.
    • Consumer API: used to subscribe to topics and process their streams of records.
    • Streams API: enables applications to behave as stream processors, which take in an input stream from topic(s) and transform it to an output stream which goes into different output topic(s).
    • Connector API: allows users to seamlessly automate the addition of another application or data system to their current Kafka topics.

Kafka: Queue and Publish-Subscriber:



Kafka: Queue and Publish-Subscriber



 


 

 

References:

  1. https://www.cloudkarafka.com/blog/part1-kafka-for-beginners-what-is-apache-kafka.html
  2. https://kafka.apache.org/documentation/
  3. https://kafka.apache.org/quickstart
  4. https://www.educba.com/kafka-architecture/?source=leftnav












Tuesday, March 1, 2022

SSO, SAML, OPID, oAuth

Terminology for oAuth:
  • User – Person Jam => User - individuals
  • Resources – email => Resource is – gmail (email)
  • Resource Owner - Jam is RO of email => Resource owner – person who have account in gmail
    • Your identity owner
  • Client –
    • the application which wants to perform the action behalf of you (RO)
  • Authorization Server –
    • which knows the resource owner, RO already has an account
  • Resource Server - Gmail (email server) => Resource server – Gmail Server
    • It is an application interface or API/Service client wants to access behalf of resource owner
  • Redirect URI –
    • this is URL, authorization server will redirect to RO back to after granting to permission to client (call back url)
  • Response Type -
    • Authorization Code – type of information client expects to receive
  • Scope –
    • Granular level information which clients wants
      • Read
      • Write
      • Update etc.
  • Consent –
    • AS takes the scope which client is requesting and verify with RO whether they want to give client permission.
  • Client id –
    • This id is used to identified client with Authorization Server
  • client secret -
    • secret pwd which client and authorization server will know
    • Securely share the information privately behind the scene
  • Auth code: to get an access token.
    • Authorization servers send this to client and this live for short time frame
    • Client will send this to Authorization Server along with client secrets in exchange to access token
    • Client will get access token
    • Client will use access token is key to communicate with Resource server (Key Card)
      • Client can perform action which Client are authorized for 

IDP and SP :

·        Identity Provider (IdP) - tool / service (login page/dashboard) that performs authentication; checking usernames and passwords, verifying account, if applicable invoking two-factor.

·        Service Provider (SP) - The web application where user is trying to gain access.

 

 


 Ã˜  OAuth design for distributed system. They play different role in OAuth solution.

v  OAuth Provider: OAuth Server or authorization server

§  Consist of three components

-        Authentication

o   Login/Login page on web page

o   Identity provider

o   OAuth is front end for identity solution

-        Request Authentication consent from user to delegation of access right to client. This is consent server. Consent server request to verify do really user want third party can access user data.

-        Token management infra – this is Database, maintained all token data which is provided for OAuth provider for validation or revocation.

v  Resource Provider’s: set of webs api

-        Makes Protected resource available via https. It can be data, service or form of web API Which provide protected data

-        Web API make sure that only authenticated/authorized user can accesses, it does through OAuth access token.

-        Check validity of provided access token, has access token is provided.

v          Resources Owner: end user

-        Owner of Protected resources, Owner of twitter account is resource owner

v          Client: Mobile or cloud app

                            -    It is an application attempt to access protect resources, linkedin trying to access facebook contact details

Ø  OAuth Actors: play specific role in OAuth to make it secure,  

v  OAuth token: these are credential, unlock something stored securely

v  OAuth endpoints: webservice/API/URLs which realize OAuth Server

v  OAuth Flows: there are 4 OAuth flow, when to use which flow

Ø  How to access APIs provided by facebook, google.

 

 

  • Client Registration is very important
  • Actual start
  • Authorization end point first to authorization code flow
  • Token end point and then access resource access
    • User - individuals
    • Resource server – Gmail Server
    • Resource is – gmail (email)
    • Resource owner – person who have account in gmail

 

oAuth Flow:

Intermediate application contact OAuth Server, OAuth Server send request to resource owner where resource owner will have entered required credential. OAuth Server validate and authenticate resource owner.

OAuth server create token and send token to Intermediate application. OAuth token (sequence of number and letters) will have resource owner details along with other details. Intermediate application send this token to resource server. Resource Server send this token to OAuth Server which get validated (active, expiry, validation etc). OAuth server send confirmation to resource server.

RS does own authorization and get resource send details to Intermediate application.

 

v  oAuth API
  • /authorize (get)
    • Authorization code (for authorization code grant)
    • Access token for implicit flow
  • /token (post)
    • Authorization: basic client-id: client-secret
    • Access token and refresh token (for Auth code Grant, Client Credential Grant and RO password credential grant

v  Resource server holds resources and protect resources

·        Resources can be protected through-

o   Via – pwd

o   By OAuth token 

v  Endpoints exist in OAuth solution

1.      1.      Authorization endpoint – provided by OAuth Server (/authorize)

a.      Based on inputs parameter

                                                    i.     Output can be either Authorization code (for authorization code grant) or Access token for implicit flow. It delivered through query parameter via redirect URI. This code is created by auth endpoint. 

2.    2.      Token endpoint – provided by OAuth Server (/token)

a.      Based on input parameter including auth code

                                                    i.     Output can be either access token and refresh token code for all flow.

                                                   ii.     Implicit grant does not use token endpoint 

3.   3.         Redirect endpoint – provided by client (redirect URI)

a.      Endpoint of the client, this is end point send info from authorization end point back to client.

                                                    i.     Input query parameter is output of Authorization end point but input to redirect endpoint this is receiving part of Auth end point (created by Auth end point) 

4.      4.      Resource endpoint – provided by resource (/api)

a.      This end point allow access to resource.

                                                    i.      Authorization: Bearer (Access Token)

 Ã˜  Token is identifiers, several types of token

Access token – use by client to access resources, client send token to resources server to access resources (validity of access token varies, and it can be reuse multiple times), it is bearer token. Based on access token resource can be access.

Refresh token – period of validity which is longer than access token. RT can be requesting to use AT. Requesting AT with RT, resource owner credential will not be validated. RT are stored and sent by client. There send to token end point of OAuth, it does not send to resource server, don’t use to refresh token to get resource it only uses to get access token.

Authorization code – auth provider creates auth code and send to client after successful authenticating resource owner and getting consent from resource owner to delegating the access. Auth code is confirmation of successful authentication and consent by the resource owner. Validity of auth code is for limited time period. client can use Auth code to request get access token from token end point 

Credential :

  • Resource owner credential – This will be provided by resource owner to Auth Server
  • Client credential: client id – client secret, this will be used when client uses token end point via
  • http basic. Every client should have client id and will be provided by Oauth provider.
  • Access token : can be treat as credential since it helps to get resources.
  • Refresh Token: allow to get new access token
  • Auth code: to get an access token.

Client Registration:

o   OAuth server/Oauth Provider, end point and get client id and client secret .

o   Client will provide to Oauth Server/Provider

o   Redirect URI

o   Required Scope – what type of information will be access

·        Client obtain from Oauth Server/Provider

o   client id

o   client secret .

  • 3rd party app contact – OAuth server (request for resource access)

 

 

OAuth Server get request – web application/API – Authorization endpoint – OAuth Server will redirect to Login Page

oAuth Flow:

OAuth Server get request – web application/API – Authorization endpoint – OAuth Server will redirect to Login Page ,  Resource owner will enter id and password which will be authenticate by login server or OAuth Server.

OAuth server will create Auth Token or Auth code (sequence of number) and send this Auth code to third party app.  Third party app use this Auth Code /token and request resource using http header and send auth code.  Resources Server get these values and send this Auth code for validation. OAuth Server will send confirmation to Resource Server. Third Party App will have access to resource owner

Authorization Code Flow Grant:

·        actors involved – Resource owner, OAuth Server, Client

Client id and client secret can be provided

·        Token can be stored in protected DB so if server-side implementation case this is recommendation if it is HTML5 or front-end application than it is not recommendation since Auth code is gateway for access.

o   Get Auth code (Authorize end point, client will request for Auth code)

o   Get the token (client uses token end point, client send auth code to token end point and get Access Token)

o   Use access token and access resources

o   Enables checking identity for 3 actors (Resource owner, OAuth Server, Client)

v  Resource owner – who owns the resource

v  Resource server – Host and protect resources

·        RW triggers this ACF.

·        Goes to OAuth server ask for auth code (Auth end point)

·        Redirect/take to login page

·        RO entered details (Authentication

·        OAuth Server send confirmation page to RO ad after confirmation third party can access RO requested details through OAuth Server.

·        Got Auth Code – next is get the token – call token endpoint



Authorization code Flow

 



 Authorization Code Grant - refresh:


this refresh token which is saved on the client side and the client can now go to the auth server and refresh the validity of this access token.
 
How does this work?
Use the refresh token, which has a longer time of validity than the access token and as an answer from the OAuth server, it receives a new access token and a new refresh token with new time to live counting from that point in time.
In this refresh flow,
1- only the token endpoint of the auth server is used.
2- The authorization endpoint is not required and the authentication of the resource owner is not needed.

the refresh token, can help to get a new access token quicker and it can help to get there quicker (than going through resource owner authentication and requiring him to enter the password again , so refresh token can be used instead). That means if a refresh token as a client and the access token has expired, it cannot access the resources on the resource server any longer, but it can use existing refresh token to go to the OAuth server and request a new access token. In contrast to the regular authorization code flow, the password of the resource owner is not required.
The refresh token tells that the resource owner has authenticated and has agreed to the access before, So that way it does not have to bother the resource owner any longer about this issue. Just use the refresh token in order to get a new access token.

Access Token is expired and Refresh token is still active

    



Note:
Dot purple flow is not valid during Refresh Token flow, but it is needed first to make sure that it should have refresh token and access token first place.


Implicit Flow Grant:

It is not using the token endpoint also not delivering any refresh tokens. This flow is used for clients that do not have the possibility to securely store the refresh token and the client ID and client secret.

This flow only have a call to the authorization endpoint and right from the authorization endpoint, it send back an access token. The token endpoint never been called, but it's only an access token that will be receive and no refresh token. The response sent back from the authorization endpoint does not contains the auth code, it contains the access token, the token type and some state. So a limiting factor of this flow is the short validity time of the access tokens, because the access token is sent directly without authenticating the client.









Client Credential Flow Grant:

The client and the resource owner are the same so it does not need to go authenticate the resource owner, because the resource owner is the client and the client is already authenticated by client ID and client secret.

The client is identical to the resource owner. Client credentials are sufficient and the resource owner is not required to authenticate. The client needs to offer secure storage for client ID, client secret and the access token and refresh tokens. Client Credential flow grant type delivers both access token and refresh token. Only one call is necessary to obtain an access token and a refresh token.



                   


Reference: 

  1. https://developer.okta.com/blog/2019/10/21/illustrated-guide-to-oauth-and-oid
  2. https://duo.com/blog/the-beer-drinkers-guide-to-saml
  3. https://www.learncsdesign.com/understanding-security-assertion-markup-language-saml/