github.com/kubearmor/cilium@v1.6.12/pkg/envoy/accesslog.go (about)

     1  // Copyright 2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package envoy
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"net/url"
    21  	"strings"
    22  
    23  	"github.com/cilium/cilium/pkg/proxy/accesslog"
    24  
    25  	"github.com/cilium/proxy/go/cilium/api"
    26  )
    27  
    28  // ParseURL returns the URL as *net.url.URL
    29  func ParseURL(scheme, host, path string) *url.URL {
    30  	u, err := url.Parse(fmt.Sprintf("%s://%s/%s", scheme, host, strings.TrimPrefix(path, "/")))
    31  	if err != nil {
    32  		u = &url.URL{
    33  			Scheme: scheme,
    34  			Host:   host,
    35  			Path:   path,
    36  		}
    37  	}
    38  	return u
    39  }
    40  
    41  // getNetHttpHeaders returns the Headers as net.http.Header
    42  func GetNetHttpHeaders(httpHeaders []*cilium.KeyValue) http.Header {
    43  	headers := make(http.Header)
    44  
    45  	for _, header := range httpHeaders {
    46  		headers.Add(header.Key, header.Value)
    47  	}
    48  
    49  	return headers
    50  }
    51  
    52  // getProtocol returns the HTTP protocol in the format that Cilium understands
    53  func GetProtocol(httpProtocol cilium.HttpProtocol) string {
    54  	switch httpProtocol {
    55  	case cilium.HttpProtocol_HTTP10:
    56  		return "HTTP/1"
    57  	case cilium.HttpProtocol_HTTP11:
    58  		return "HTTP/1.1"
    59  	case cilium.HttpProtocol_HTTP2:
    60  		return "HTTP/2"
    61  	default:
    62  		return "Unknown"
    63  	}
    64  }
    65  
    66  // GetFlowType returns the type of flow (request|response)
    67  func GetFlowType(m *cilium.LogEntry) accesslog.FlowType {
    68  	// the fall back type is request
    69  	result := accesslog.TypeRequest
    70  
    71  	if m != nil {
    72  		switch m.EntryType {
    73  		case cilium.EntryType_Denied:
    74  			result = accesslog.TypeRequest
    75  		case cilium.EntryType_Request:
    76  			result = accesslog.TypeRequest
    77  		case cilium.EntryType_Response:
    78  			result = accesslog.TypeResponse
    79  		}
    80  	}
    81  
    82  	return result
    83  }
    84  
    85  // GetVerdict returns the verdict performed on the flow (forwarded|denied)
    86  func GetVerdict(m *cilium.LogEntry) accesslog.FlowVerdict {
    87  	// the default verdict is forwarded
    88  	result := accesslog.VerdictForwarded
    89  
    90  	if m != nil {
    91  		switch m.EntryType {
    92  		case cilium.EntryType_Denied:
    93  			result = accesslog.VerdictDenied
    94  		}
    95  	}
    96  
    97  	return result
    98  }