"use strict";(self.webpackChunkgrafana=self.webpackChunkgrafana||[]).push([[3082],{91948:(ne,g,a)=>{a.r(g),a.d(g,{ApiKeysPageUnconnected:()=>C,default:()=>ee});var e=a(68404),M=a(36635),T=a(37932),h=a(52081),w=a(81764),D=a(8944),m=a(79396),E=a(77582),S=a(25405),A=a(81168),b=a(14747);const N=({searchQuery:t,disabled:n,onSearchChange:r})=>e.createElement("div",{className:"page-action-bar"},e.createElement("div",{className:"gf-form gf-form--grow"},e.createElement(b.H,{placeholder:"Search keys",value:t,onChange:r})));var d=a(9892),f=a(38849),v=a(72648),k=a(6554),F=a(39904),x=a(31403),B=a(13626);const L=({apiKeys:t,timeZone:n,onDelete:r,onMigrate:l})=>{const o=(0,v.l4)(),i=$(o);return e.createElement("table",{className:"filter-table"},e.createElement("thead",null,e.createElement("tr",null,e.createElement("th",null,"Name"),e.createElement("th",null,"Role"),e.createElement("th",null,"Expires"),e.createElement("th",null,"Last used at"),e.createElement("th",{style:{width:"34px"}}))),t.length>0?e.createElement("tbody",null,t.map(s=>{const c=Boolean(s.expiration&&Date.now()>new Date(s.expiration).getTime());return e.createElement("tr",{key:s.id,className:i.tableRow(c)},e.createElement("td",null,s.name),e.createElement("td",null,s.role),e.createElement("td",null,R(s.expiration,n),c&&e.createElement("span",{className:i.tooltipContainer},e.createElement(k.u,{content:"This API key has expired."},e.createElement(F.J,{name:"exclamation-triangle"})))),e.createElement("td",null,Q(n,s.lastUsedAt)),e.createElement("td",null,e.createElement(h.Lh,{justify:"flex-end"},e.createElement(x.zx,{size:"sm",onClick:()=>l(s)},"Migrate to service account"),e.createElement(B.m,{"aria-label":"Delete API key",size:"sm",onConfirm:()=>r(s),disabled:!E.Vt.hasPermissionInMetadata(A.AccessControlAction.ActionAPIKeysDelete,s)}))))})):null)};function Q(t,n){return n?(0,f.dq)(n,{timeZone:t}):"Never"}function R(t,n){return t?(0,f.dq)(t,{timeZone:n}):"No expiration date"}const $=t=>({tableRow:n=>d.css`
    color: ${n?t.colors.text.secondary:t.colors.text.primary};
  `,tooltipContainer:d.css`
    margin-left: ${t.spacing(1)};
  `});var K=a(45253),z=a(98102);const U=({onMigrate:t,apikeysCount:n,disabled:r})=>{const[l,o]=(0,e.useState)(!1),i=(0,v.wW)(V),s=e.createElement("a",{className:"external-link",href:"https://grafana.com/docs/grafana/latest/administration/api-keys/#migrate-api-keys-to-grafana-service-accounts",target:"_blank",rel:"noopener noreferrer"},"Find out more about the migration here."),c=e.createElement("span",null,"Migrating all API keys will hide the API keys tab.",e.createElement("br",null),e.createElement("br",null),"The API keys API will remain available for you to create new API keys, but we strongly encourage you to use Service accounts instead.");return e.createElement(e.Fragment,null,n>0&&e.createElement(K.b,{title:"Switch from API keys to service accounts",severity:"warning"},e.createElement("div",{className:i.text},"We will soon deprecate API keys. Each API key will be migrated into a service account with a token and will continue to work as they were. We encourage you to migrate your API keys to service accounts now. ",s),e.createElement("div",{className:i.actionRow},e.createElement(x.zx,{className:i.actionButton,onClick:()=>o(!0)},"Migrate all service accounts"),e.createElement(z.s,{title:"Migrate API keys to Service accounts",isOpen:l,body:c,confirmText:"Yes, migrate now",onConfirm:t,onDismiss:()=>o(!1),confirmVariant:"primary",confirmButtonVariant:"primary"}))),n===0&&e.createElement(e.Fragment,null,e.createElement(K.b,{title:"No API keys found",severity:"warning"},e.createElement("div",{className:i.text},"No API keys were found. If you reload the browser, this tab will be not available. If any API keys are created, this tab will appear again."))))},V=t=>({text:d.css`
    margin-bottom: ${t.spacing(2)};
  `,actionRow:d.css`
    display: flex;
    align-items: center;
  `,actionButton:d.css`
    margin-right: ${t.spacing(2)};
  `});var u=a(29930),y=a(56777);function p(){return async t=>{t((0,y.dF)());const[n,r]=await Promise.all([(0,u.i)().get("/api/auth/keys?includeExpired=false&accesscontrol=true"),(0,u.i)().get("/api/auth/keys?includeExpired=true&accesscontrol=true")]);t((0,y.iK)({keys:n,keysIncludingExpired:r}))}}function O(t){return async n=>{(0,u.i)().delete(`/api/auth/keys/${t}`).then(()=>n(p()))}}function W(t){return async n=>{try{await(0,u.i)().post(`/api/serviceaccounts/migrate/${t}`)}finally{n(p())}}}function j(){return async t=>{try{await(0,u.i)().post("/api/serviceaccounts/migrate")}finally{t(p())}}}function Z(){return t=>{t((0,y.j4)())}}const H=t=>t.includeExpired?t.keysIncludingExpired.length:t.keys.length,J=t=>{const n=RegExp(t.searchQuery,"i");return(t.includeExpired?t.keysIncludingExpired:t.keys).filter(l=>n.test(l.name)||n.test(l.role))},Y=t=>t.includeExpired,G=t=>t.keys.length===0&&t.keysIncludingExpired.length>0;function X(t){const n=E.Vt.hasAccess(A.AccessControlAction.ActionAPIKeysCreate,!0);return{apiKeys:J(t.apiKeys),searchQuery:t.apiKeys.searchQuery,apiKeysCount:H(t.apiKeys),hasFetched:t.apiKeys.hasFetched,timeZone:(0,S.Z)(t.user),includeExpired:Y(t.apiKeys),includeExpiredDisabled:G(t.apiKeys),canCreate:n}}const I={navId:"apikeys"},q={loadApiKeys:p,deleteApiKey:O,migrateApiKey:W,migrateAll:j,setSearchQuery:y.ql,toggleIncludeExpired:Z},_=(0,M.connect)(X,q);class C extends e.PureComponent{constructor(n){super(n),this.onDeleteApiKey=r=>{this.props.deleteApiKey(r.id)},this.onMigrateAll=()=>{this.props.migrateAll()},this.onMigrateApiKey=r=>{this.props.migrateApiKey(r.id)},this.onSearchQueryChange=r=>{this.props.setSearchQuery(r)},this.onIncludeExpiredChange=r=>{this.props.toggleIncludeExpired()},this.onMigrateApiKeys=async()=>{try{this.onMigrateAll();let r="/org/serviceaccounts";T.E1.push(r),window.location.reload()}catch(r){console.error(r)}}}componentDidMount(){this.fetchApiKeys()}async fetchApiKeys(){await this.props.loadApiKeys()}render(){const{hasFetched:n,apiKeysCount:r,apiKeys:l,searchQuery:o,timeZone:i,includeExpired:s,includeExpiredDisabled:c,canCreate:te}=this.props;if(!n)return e.createElement(m.T,{...I},e.createElement(m.T.Contents,{isLoading:!0}));const P=r>0;return e.createElement(m.T,{...I},e.createElement(m.T.Contents,{isLoading:!1},e.createElement(e.Fragment,null,e.createElement(U,{onMigrate:this.onMigrateApiKeys,apikeysCount:r}),P?e.createElement(N,{searchQuery:o,disabled:!te,onSearchChange:this.onSearchQueryChange}):null,P?e.createElement(h.wc,null,e.createElement(w._,{disabled:c,label:"Include expired keys"},e.createElement(D.x,{id:"showExpired",value:s,onChange:this.onIncludeExpiredChange})),e.createElement(L,{apiKeys:l,timeZone:i,onMigrate:this.onMigrateApiKey,onDelete:this.onDeleteApiKey})):null)))}}const ee=_(C)}}]);

//# sourceMappingURL=ApiKeysPage.acf8ac6c95c61ab7f145.js.map